Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore fieldvalue.increment using setData does not increment the value, while updateData does

I have an existing document with count value as 1. When i use FieldValue.increment(1) using setData (which should overwrite with new incremented value), it does not increment the value, but using the same with updateData, increments the value to 2.

Why is that? What is the difference between setData and updateData in this case, especially when I am updating with same number of attributes?

like image 787
veenatic Avatar asked Mar 04 '26 23:03

veenatic


2 Answers

"set" type operations overwrite existing data by default, so it's working the way you expect. It does not consider the values of any existing fields.

If you add the "merge" option to setData, then only the specified fields will get updated, and everything else will stay the same, like update. See the documentation for setData.

ref.setData(data, true)  // merge is true here
like image 170
Doug Stevenson Avatar answered Mar 06 '26 14:03

Doug Stevenson


You use setData to set a new value, if it's already set it will just override with the new value without looking at the existing. On the other hand updateData updates the value, just as it should.

like image 22
Sergei Emelianov Avatar answered Mar 06 '26 14:03

Sergei Emelianov



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!