首页  >  文章  >  web前端  >  如何高效更新 Firestore 中的对象数组?

如何高效更新 Firestore 中的对象数组?

Linda Hamilton
Linda Hamilton原创
2024-10-26 05:58:30769浏览

How to Efficiently Update Arrays of Objects in Firestore?

更新 Firestore 中的对象数组

更新 Firestore 中的对象数组可能是一个简单的过程,但它需要使用Firestore SDK 中的特定方法。

要将新记录追加到对象数组中,可以使用 arrayUnion() 方法。此方法采用一个元素数组作为其参数,并将这些元素添加到现有数组中,但前提是它们尚不存在。

以下代码演示了如何使用 arrayUnion( ):

firebase.firestore()
.collection('proprietary')
.doc(docID)
.update({ sharedWith: firebase.firestore.FieldValue.arrayUnion({ who: "[email protected]", when: new Date() }) })

arrayRemove() 方法可用于从数组中删除元素。它接受一个元素数组作为参数,并删除每个给定元素的所有实例。

要从sharedWith数组中删除特定元素,可以使用以下代码:

firebase.firestore()
.collection('proprietary')
.doc(docID)
.update({ sharedWith: firebase.firestore.FieldValue.arrayRemove({ who: "[email protected]" }) })

这些方法提供了一种方便高效的方法来更新 Firestore 中的数组,确保仅添加或删除指定的元素,而使数组的其余部分保持不变。

以上是如何高效更新 Firestore 中的对象数组?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn