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

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

问题描述

我目前正在尝试 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...

推荐答案

Edit 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.

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

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