Write data in to nested object in firebase firestore

我的未来我决定 提交于 2021-01-28 07:10:01

问题


I have a data structure that looks as follows:

This is the top level of the collection:

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 }
);

But that leaves me with:

Please help. Changing the structure of pages is an option.

This is what I've tried to replicate: Update fields in nested objects in firestore documents?


回答1:


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 }
);

This will affect the element in the Array Images, the element image.position its value count.



来源:https://stackoverflow.com/questions/63527492/write-data-in-to-nested-object-in-firebase-firestore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!