Batch writes
Instead of performing multiple individual writes, we gather them in a batch object and ask Firebase to commit all the writes at once. A single network request is sent.
It is atomic: if one write fails, the others fail as well. This prevents a broken state where only some documents are updated.
batch update from the client
Collect up to 500 writes in a batch object, then execute the batch with commit()
const batch = writeBatch(db)
batch.update(docRef1, { timezone: "Europe/London" })
batch.update(docRef2, { timezone: "Europe/London" })
await batch.commit()
batch update from the Admin SDK
In the admin SDK, we get a batch helper differently. The remaining code is the same.
const batch = db.batch()
// same code
other batch operations
batch.set(docRef, data)
batch.set(docRef, data, { merge: true })
batch.update(docRef, data)
batch.delete(docRef)
batch.create(docRef, data) // Admin SDK