Reputation: 83
I want to change the value of a certain field of all documents in a Cloud Firestore collection to a certain value if that field is equal to a certain value. How do I do that?
Upvotes: 1
Views: 117
Reputation: 138824
In addition to Doug's answer, if you want to update all documents in a collection where a field contains a certain value, then please use the following lines of code:
FirebaseFirestore db = FirebaseFirestore.getInstance();
Query query = db.collection("collName").whereEqualTo("fieldName", "fieldValue");
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
document.getReference().update("fieldToUpdate", "value");
}
}
}
});
Upvotes: 1
Reputation: 317362
Upvotes: 1