如何更新“对象数组”与Firestore? [英] How to update an "array of objects" with Firestore?

查看:129
本文介绍了如何更新“对象数组”与Firestore?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试Firestore,而且我遇到了一些非常简单的事情:更新一个数组(也就是一个子文档)。

I'm currently trying Firestore, and I'm stuck at something very simple: "updating an array (aka a subdocument)".

我的数据库结构是超级简单。例如:

My DB structure is super simple. For example:

proprietary: "John Doe"
sharedWith:
  [
    {who: "first@test.com", when:timestamp}
    {who: "another@test.com", when:timestamp}
  ]

我正在尝试(没有成功)将新记录推送到 shareWith 对象数组。

I'm trying (without success) to push new records into shareWith array of objects.

我试过:

// With SET
firebase.firestore()
.collection('proprietary')
.doc(docID)
.set(
  { sharedWith: [{ who: "third@test.com", when: new Date() }] },
  { merge: true }
)

// With UPDATE
firebase.firestore()
.collection('proprietary')
.doc(docID)
.update({ sharedWith: [{ who: "third@test.com", when: new Date() }] })

无效。
这些查询会覆盖我的数组。

None works. These queries overwrite my array.

答案可能很简单,但我找不到......

The answer might be simple, but I could'nt find it...

谢谢

推荐答案

编辑08/13/2018 :现在有支持用于Cloud Firestore中的本机阵列操作。请参阅下面的道格答案

Edit 08/13/2018: There is now support for native array operations in Cloud Firestore. See Doug's answer below.

目前无法在Cloud Firestore中更新单个数组元素(或添加/删除单个元素)。

There is currently no way to update a single array element (or add/remove a single element) in Cloud Firestore.

此处代码:

firebase.firestore()
.collection('proprietary')
.doc(docID)
.set(
  { sharedWith: [{ who: "third@test.com", when: new Date() }] },
  { merge: true }
)

这说到将文档设置为 proprietary / docID ,以便 sharedWith = [{who:third@test.com,when:new Date()} 但不影响任何现有文档属性。它与您提供的 update()调用非常相似,但是 set()调用并创建文档(如果有) update()调用失败时不存在。

This says to set the document at proprietary/docID such that sharedWith = [{ who: "third@test.com", when: new Date() } but to not affect any existing document properties. It's very similar to the update() call you provided however the set() call with create the document if it does not exist while the update() call will fail.

因此,您有两个选项可以实现您想要的效果。

So you have two options to achieve what you want.

选项1 - 设置整个数组

致电 set()包含数组的全部内容,这将需要首先从数据库中读取当前数据。如果您担心并发更新,您可以在交易中完成所有这些。

Call set() with the entire contents of the array, which will require reading the current data from the DB first. If you're concerned about concurrent updates you can do all of this in a transaction.

选项2 - 使用子收藏

您可以使 sharedWith 主文档的子集合。然后
添加单个项目将如下所示:

You could make sharedWith a subcollection of the main document. Then adding a single item would look like this:

firebase.firestore()
  .collection('proprietary')
  .doc(docID)
  .collection('sharedWith')
  .add({ who: "third@test.com", when: new Date() })

当然,这会带来新的限制。您将无法根据共享对象来查询
文档,也无法获得
获取文档以及所有 sharedWith 单次操作中的数据。

Of course this comes with new limitations. You would not be able to query documents based on who they are shared with, nor would you be able to get the doc and all of the sharedWith data in a single operation.

这篇关于如何更新“对象数组”与Firestore?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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