Is there any way to increment existing value in firestore ?
What I am trying. FirebaseFirestore.getInstance()
.collection("notifications")
.document(response.getString("multicast_id"))
.set(notificationData);
FirebaseFirestore.getInstance()
.collection("users")
.document(post.userId)
.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
int followers = Integer.valueOf(task.getResult().getData().get("followers").toString());
FirebaseFirestore.getInstance()
.collection("users")
.document(post.userId).update("followers", followers++).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
}
});
}
}
});
Database Structure
users
- data....
- followers(number)
According to this Answer function available for increment or decrement numeric field values
Version 5.9.0 - Mar 14, 2019
Added FieldValue.increment(), which can be used in update() and set(..., {merge:true}) to increment or decrement numeric field values safely without transactions.
https://firebase.google.com/support/release-notes/js
Is any function available for incrementing existing value in firestore android?