10

so i've read all the documentation everything about increment a counter in a Firestore database. I have this code

const admin = require("firebase-admin");
const db = admin.firestore();
...
db
.collection("settings")
.doc("totalUsers")
.set({
count: firebase.firestore.FieldValue.increment(1),
});

And i just doesn't increment the counter at all. No errors no logs no nothing. In my Firestore i have a collection of settings and a document totalUsers with a property count that is a number type and it equals to 1.

Am I doing something wrong? Am I missing anything? Any help appreciated!

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Constantin Chirila
  • 1,979
  • 2
  • 19
  • 33

4 Answers4

13

increment works with both update and set.
For it to actually increment the field you can:

  • use update, but the documents must exist already
  • use set with option merge: true or mergeFields: "something", with something the name of the field being incremented
Louis Coulet
  • 3,663
  • 1
  • 21
  • 39
  • This is the most complete answer. – John T Jul 15 '21 at 05:24
  • 1
    Yes, if you don't use the `{ merge: true }` option with `set`, `firebase.firestore.FieldValue.increment(0)` will set your field to zero instead of performing **NO INCREMENT**. This is particularly useful when your incremental value can be a variable with situation that it can be sometimes ZERO. – Antonio Ooi Jun 21 '22 at 08:43
  • for nested fields it's now a requirement to use set with merge true. Update does not work and this is not reflected in the firebase docs – van Jan 26 '23 at 15:25
  • I just checked and it still works with ```update```, you need to use the dot notation to update nested fields: https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects – Louis Coulet Jan 26 '23 at 20:56
5

For anyone having issues with this, be careful to use the .update() method instead .set(). Also you need to have a value existing in the firestore that so it has something to increment.

Constantin Chirila
  • 1,979
  • 2
  • 19
  • 33
  • 2
    I have a similar issue and the funny thing is I am using FieldValue increment as a set() operation on an oncreate trigger. But for another function, i am using onwrite trigger and Fieldvalue increment does not work. But in the documentation it says works with set and update – Archid Aug 14 '20 at 21:45
  • I had a missing value, so update was trying to increment **nothing**. Thanks for help! – drpawelo Jun 29 '22 at 20:37
3

Since you are using the Admin SDK, you should do as follows:

count: admin.firestore.FieldValue.increment(1)
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
0

Try with this: (this is my code and works for me)

var firebaseUser = await FirebaseAuth
                        .instance
                        .currentUser();
DocumentReference query = Firestore.instance
                              .collection("users")
                              .document(firebaseUser.uid);
                                                    Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => LoginView()));
 var loginSuccess = wait model.getdata(_inputCuit.text);

 if (loginSuccess) {
     query.updateData({ "queries": FieldValue.increment(1)});
 }

The key area is the .increment() method

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459