Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Firestore Increment FieldValue does not increment

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!

like image 270
Constantin Chirila Avatar asked Apr 13 '20 13:04

Constantin Chirila


2 Answers

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
like image 164
Louis Coulet Avatar answered Oct 22 '22 18:10

Louis Coulet


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.

like image 5
Constantin Chirila Avatar answered Oct 22 '22 16:10

Constantin Chirila