0

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?

Wan33
  • 163
  • 1
  • 12

1 Answers1

2

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)
});
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84