更新 Firestore 文档中嵌套对象中的字段? [英] Update fields in nested objects in firestore documents?
问题描述
我有一个数据结构,如:
我想编辑first"对象中test"键的值.我遵循了
test2"键不见了.但是,我只想更新test"的值并保留test2".
这个问题有什么解决方案吗?
根据你提供的链接,它是这样写的:
<块引用>如果您的文档包含嵌套对象,您可以在调用 update() 时使用点表示法"来引用文档中的嵌套字段:
因此你需要使用dot notation
才能只更新一个字段而不覆盖,就像这样:
var setAda = dbFirestore.collection('users').doc('alovelace').update({"first.test": "12345"});
那么你将拥有:
首先测试:12345"测试2:abcd"
I have a data structure like:
I want to edit the value of "test" key in "first" object. I followed the document on https://firebase.google.com/docs/firestore/manage-data/add-data
But it did not work for me.
The nodejs code:
var setAda = dbFirestore.collection('users').doc('alovelace').update({
first : {
test: "12345"
}
});
The result in firestore:
The "test2" key was gone. However, I only want to update the value of "test" and keep the "test2".
Any solution for this problem?
According to the link you provided, it says this:
If your document contains nested objects, you can use "dot notation" to reference nested fields within the document when you call update():
Therefore you need to use dot notation
to be able to update only one field without overwriting, so like this:
var setAda = dbFirestore.collection('users').doc('alovelace').update({
"first.test": "12345"
});
then you will have:
first
test: "12345"
test2: "abcd"
这篇关于更新 Firestore 文档中嵌套对象中的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!