Basically, I would like to know if it is possible to update all of the documents found in a collection in Firestore. I am able to get all the documents in a list like so:
<
Raj's method will work but may lead to doing a lot of writes. As Doug mentioned, you may like to do it in a batch write.
void getData() {
mFirebaseFirestore.collection("Events").get().addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
List list = new ArrayList<>();
for (QueryDocumentSnapshot document : task.getResult()) {
list.add(document.getId());
}
Log.d(TAG, list.toString());
updateData(list); // *** new ***
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
}
void updateData(ArrayList list) {
// Get a new write batch
WriteBatch batch = db.batch();
// Iterate through the list
for (int k = 0; k < list.size(); k++) {
// Update each list item
DocumentReference ref = db.collection("Events").document(list.get(k));
batch.update(ref, "field_you_want_to_update", "new_value");
}
// Commit the batch
batch.commit().addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
// Yay its all done in one go!
}
});
}
Documentation: https://firebase.google.com/docs/firestore/manage-data/transactions#batched-writes