I'm using Cloud Firestore and have a collection of documents. For each document in the collection I would like to update one of the fields.
Using a transaction to perform the update would be inefficient because I do not need to read any of the data while updating it.
Batch updates seem like the right direction, however the docs do not include examples of updating multiple docs at once. See here: Batched Writes
Firestore Update Entire DocumentgetDatabase() → where we want to update a document. doc() → where we'll be passing references of database, collection and ID of a document that we want to update. setDoc() → where we actually pass new data that we want to replace along with the doc() method.
There are two types of atomic operations in Cloud Firestore: Transactions: a transaction is a set of read and write operations on one or more documents. Batched Writes: a batched write is a set of write operations on one or more documents.
Step 1: Import CSV file and select fields You can create a table for existing Firestore collection or create database collection right from the Rowy UI. Then CSV import using the import data icon. For brand new collections, with import data you can create the table structure as well with ease.
If you have used Firebase database, writing to completely single separate locations atomically was not possible, that's why you would have to use batch writes, which means that either all of the operations succeed, or none of them are applied.
Regarding Firestore, all operations are now atomically processed. However, you can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations. A batch of writes completes atomically and can write to multiple documents.
This a simple example regarding a batch operation for write, update and delete operation.
WriteBatch batch = db.batch();
DocumentReference johnRef = db.collection("users").document("John");
batch.set(johnRef, new User());
DocumentReference maryRef = db.collection("users").document("Mary");
batch.update(maryRef, "Anna", 20); //Update name and age
DocumentReference alexRef = db.collection("users").document("Alex");
batch.delete(alexRef);
batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// ...
}
});
Calling commit()
method on the batch object means that you commit the entire batch.
I was looking for a solution, found none, so I made this one, if anyone's interested.
public boolean bulkUpdate() {
try {
// see https://firebase.google.com/docs/firestore/quotas#writes_and_transactions
int writeBatchLimit = 500;
int totalUpdates = 0;
while (totalUpdates % writeBatchLimit == 0) {
WriteBatch writeBatch = this.firestoreDB.batch();
List<QueryDocumentSnapshot> documentsInBatch =
this.firestoreDB.collection("animals")
.whereEqualTo("species", "cat")
.limit(writeBatchLimit)
.get()
.get()
.getDocuments();
if (documentsInBatch.isEmpty()) {
break;
}
documentsInBatch.forEach(
document -> writeBatch.update(document.getReference(), "hasTail", true));
writeBatch.commit().get();
totalUpdates += documentsInBatch.size();
}
System.out.println("Number of updates: " + totalUpdates);
} catch (Exception e) {
return false;
}
return true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With