如何创建数组(如果尚不存在),然后将数据附加到Firestore中的数组? [英] How to create array if not already present and then append data to the array in Firestore?

查看:46
本文介绍了如何创建数组(如果尚不存在),然后将数据附加到Firestore中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参考"更新数组中的元素:

我可以理解如何将元素添加到现有数组.但是,我想创建一个新数组(如果尚不存在),然后在单个Firestore调用中向其中添加数据.该怎么做?

I can understand how to add elements to an existing array. But I would like to create a new array if doesn't already exist and then add data to it in a single Firestore call. How to do that?

  1. 如果不存在数组则不添加数据.

.update("arrayName",FieldValue.arrayUnion("arrayData"))

创建一个新数组(如果不存在)并添加数据,但数据将被覆盖而不附加.

.set(hashMapOf("arrayName" to arrayListOf("arrayData")),SetOptions.merge())

我要拨打单个 Firestore呼叫

I want to make a single Firestore call,

  1. 如果不创建新数组,将检查是否存在数组.
  2. 然后,如果没有将数据添加到数组中,它将检查数组中是否存在数据.

是否有解决此问题的直接方法,还是我们必须打一个以上电话才能实现?

Is there a straightforward solution to this or do we have to make more than one call to achieve this?

我正在使用的代码:

return firebaseFirestore
  .collection("collection1")
  .document("document1")
  .collection("collection2")
  .document("document2")
  .update("arrayName", FieldValue.arrayUnion("arrayData"))

如您所指出的,

@Alex,如果不存在则创建数组,并向其中添加数据.发现我的问题是由其他问题引起的.

@Alex as you pointed out, the array is created if not present, and data is added to it. Found my problem to be caused due to a different issue.

在我的场景中,"collection1","document1","collection2"和"document2"尚未创建.只有在要向其添加数组的文档(文档")已经创建的情况下,我的代码才有效.

In my scenario, the "collection1", "document1", "collection2" and "document2" were not already created. My code works only if the document to which the array is to be added ("document") is already created.

您能帮我解决这个问题吗?

Can you help me in solving this?

推荐答案

  1. 如果不存在数组则不添加数据.

是的,确实如此.根据有关FieldValue的 arrayUnion()方法:

Yes, it does. According to the official documentation regarding FieldValue's arrayUnion() method:

返回一个可以与 set() update()一起使用的特殊值,该值告诉服务器将给定元素与给定元素结合在一起服务器.数组中尚不存在的每个指定元素都将添加到末尾.如果要修改的字段还不是数组,它将被包含指定元素的数组覆盖.

Returns a special value that can be used with set() or update() that tells the server to union the given elements with any array value that already exists on the server. Each specified element that doesn't already exist in the array will be added to the end. If the field being modified is not already an array it will be overwritten with an array containing exactly the specified elements.

换句话说,如果您的 arrayName 属性不是数组,那么您的 arrayData 将被添加到数组中,然后该数组将在文档中被覆盖.因此,如果您的 arrayName 不是数组,它将成为数组.如果该属性根本不存在,则将创建一个新数组并将其添加到文档中.因此,如果您的文档如下所示:

In other words, if your arrayName property is not an array, your arrayData will be added to an array and then that array will be overwritten in the document. So if your arrayName is not an array, it will become an array. If the property does not exist at all, a new array will be created and it will be added to the document. So if your document looks like this:

Firestore-root
   |
   --- firstCollection
          |
          --- firstDocument
               |
               --- docName: "Doc Name"

使用以下几行代码后:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference docRef = rootRef.collection("firstCollection").document("firstCollection");
docRef.update("arrayName", FieldValue.arrayUnion("arrayData"));

您的文档将如下所示:

Firestore-root
   |
   --- firstCollection
          |
          --- firstDocument
               |
               --- docName: "Doc Name"
               |
               --- arrayName: ["arrayData"]

仅当要向其添加数组的文档(文档")已创建时,我的代码才有效.

My code works only if the document to which the array is to be added ("document") is already created.

是的,这是预期的行为.您无法更新不存在的文档中的数组.为了解决这个问题,您基本上应该创建该文档,至少要有一个属性,以便该文档存在于该特定位置.该属性可能是空数组或任何其他支持的数据类型,因为它将稍后转换为数组.

Yes, that's the expected behavior. You cannot update an array in a document that does not exist. To solve this, you should basically create that document, with at least a property so it exists at that particular location. The property might be an empty array or any other supported data-type, as it will be converted later into an array.

更合适的解决方案可能是检查文档是否存在.如果存在,则将其添加到所需的数组,否则,对其进行更新.但是,您不可能一口气做到这一点.

A more appropriate solution might be to check if the document exists. If it exists, add it with the desired array, otherwise, update it. But there is no way you can do that in a single go.

这篇关于如何创建数组(如果尚不存在),然后将数据附加到Firestore中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆