0

I have a data structure that looks as follows: enter image description here

This is the top level of the collection: enter image description here

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: enter image description here

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?

qwertz
  • 59
  • 4

1 Answers1

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.

Soni Sol
  • 2,367
  • 3
  • 12
  • 23