Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use FieldValue.increment() on the property of an object type document field

Firestore's FieldValue.increment(someValue) is working properly with other fields but not with map.

I am trying to increment the value of a property in a map. I have a document named user. It has a map points:{id:123, total:100}. I want to update the value of total in map points.

firestore.collection('users').doc('user').update({
    "name": "atul",
    "points": { "total": firestore.FieldValue.increment(50) }
})

Instead of incrementing the total value to 150, its value becomes 100.

like image 213
atultherajput Avatar asked Jan 26 '26 15:01

atultherajput


1 Answers

You'll need to call out the full path of the field to increment, not bury it inside an object. The full path of the field involves using . as a separator between the names of each field:

firestore.doc(...).update({
    "points.total": firestore.FieldValue.increment(50)
})
like image 64
Doug Stevenson Avatar answered Jan 28 '26 04:01

Doug Stevenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!