将数据写入Firebase Firestore中的嵌套对象 [英] Write data in to nested object in firebase firestore
问题描述
我有一个数据结构,如下所示:
I have a data structure that looks as follows:
这是收藏集的顶层:
我想写些增加字段数的记录,但我做不到.我尝试了许多不同的方法,所以我不想遍历所有方法.我最接近的是通过:
I want to write to increment the field count but I can't do it. I've tried so many different methods that I'd rather not go through all of them. The closest I've gotten was through:
const pageRef = admin.firestore().collection("pages").doc(image.page);
pageRef.set(
{
[`images.${image.position}.count`]: admin.firestore.FieldValue.increment(
1
),
},
{ merge: true }
);
但是那让我有了:
请帮助.更改页面的结构是一种选择.
Please help. Changing the structure of pages is an option.
这是我尝试复制的内容:更新firestore文档中嵌套对象中的字段?
This is what I've tried to replicate: Update fields in nested objects in firestore documents?
推荐答案
问题在于如何使用点通知.
The issue is on how the point notaition is being used.
在帖子中,您分享了他们使用的示例是:
In the Post you shared the example they use is:
var setAda = dbFirestore.collection('users').doc('alovelace').update({
"first.test": "12345"
});
将其应用于您的代码和模型将是:
Applying this to your Code and model would be:
const pageRef = admin.firestore().collection("pages").doc(image.page);
pageRef.set(
{
`images[${image.position}].count`: admin.firestore.FieldValue.increment(
1
),
},
{ merge: true }
);
这将影响数组图像"中的元素,即元素 image.position
的值计数.
This will affect the element in the Array Images, the element image.position
its value count.
这篇关于将数据写入Firebase Firestore中的嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!