首页  >  文章  >  web前端  >  如何在不覆盖整个数组的情况下有效更新 Firestore 文档中的数组?

如何在不覆盖整个数组的情况下有效更新 Firestore 文档中的数组?

DDD
DDD原创
2024-10-25 06:56:29446浏览

How can I efficiently update arrays within Firestore documents without overwriting the whole array?

使用 Firestore 更新数组

Firestore 提供了一种简单的方法来更新文档中的数组,无需覆盖整个数组。

以下函数启用数组更新:

  • arrayUnion(): 将元素添加到数组,确保不包含重复项。
  • arrayRemove(): 从数组中删除指定元素的所有实例。

示例代码:

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

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

这些函数提供了一种简单有效的管理方法Firestore 中的数组,允许开发者进行有针对性的更新而不影响整个数组。

以上是如何在不覆盖整个数组的情况下有效更新 Firestore 文档中的数组?的详细内容。更多信息请关注PHP中文网其他相关文章!

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