Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore - How to decrement integer value atomically in database?

Firestore has recently launched a new feature to increment and decrement the integer values atomically.

I can increment the integer value using

For instance, FieldValue.increment(50)

But how to decrement ?

I tried using FieldValue.decrement(50) But there is no method like decrement in FieldValue.

It's not working.

https://firebase.googleblog.com/2019/03/increment-server-side-cloud-firestore.html?linkId=65365800&m=1

like image 886
A K Avatar asked Apr 10 '19 21:04

A K


People also ask

How do I increment a number in firestore?

Firestore now has a specific operator for this called FieldValue. increment() . By applying this operator to a field, the value of that field can be incremented (or decremented) as a single operation on the server.


1 Answers

To decrement the value in a field, do a negative increment. So:

FieldValue.increment(-50) 
like image 159
Frank van Puffelen Avatar answered Sep 30 '22 01:09

Frank van Puffelen