'Math Functions With Firebase Firestore [duplicate]

Each time I click a button on my site I would like to 'updateDoc' increase the value of the number in the database with something similar the the Javascript ++

I was wondering if this is possible and if its not what else can i do?



Solution 1:[1]

Firestore has a increment() function that you can use to increment a field while updating a document.

import { doc, updateDoc, increment } from "firebase/firestore";

const docRef = doc(db, "collection", "DOC_ID");

await updateDoc(docRef, {
    field_name: increment(1)
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharmaraj