我目前正在尝试 Firestore,但我陷入了一些非常简单的事情:“更新数组(又名子文档)”。
我的数据库结构非常简单。例如:
proprietary: "John Doe",
sharedWith:
[
{who: "[email protected] /cdn-cgi/l/email-protection", when:timestamp},
{who: "[email protected] /cdn-cgi/l/email-protection", when:timestamp},
],
我正在尝试(但没有成功)将新记录推入shareWith
对象数组。
我试过了:
// With SET
firebase.firestore()
.collection('proprietary')
.doc(docID)
.set(
{ sharedWith: [{ who: "[email protected] /cdn-cgi/l/email-protection", when: new Date() }] },
{ merge: true }
)
// With UPDATE
firebase.firestore()
.collection('proprietary')
.doc(docID)
.update({ sharedWith: [{ who: "[email protected] /cdn-cgi/l/email-protection", when: new Date() }] })
没有一个有效。这些查询会覆盖我的数组。
答案可能很简单,但我找不到......
Firestore 现在有两个函数,允许您更新数组而无需重写整个数组。
Link: https://firebase.google.com/docs/firestore/manage-data/add-data https://firebase.google.com/docs/firestore/manage-data/add-data, 具体来说https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array
更新数组中的元素
如果您的文档包含数组字段,您可以使用arrayUnion()
and
arrayRemove()
添加和删除元素。arrayUnion()
添加元素
到一个数组,但仅限于尚未存在的元素。arrayRemove()
删除每个给定元素的所有实例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)