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?
"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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With