0
0
FirebaseHow-ToBeginner · 3 min read

How to Use Batch Write in Firestore: Simple Guide

Use writeBatch() from Firestore to create a batch object, then add multiple set, update, or delete operations to it. Finally, call commit() on the batch to apply all changes atomically in one request.
📐

Syntax

The batch write process involves three main steps:

  • Create a batch: Use const batch = firestore.batch() to start.
  • Add operations: Use batch.set(docRef, data), batch.update(docRef, data), or batch.delete(docRef) to queue changes.
  • Commit the batch: Call batch.commit() to apply all queued operations atomically.
javascript
const batch = firestore.batch();

const docRef1 = firestore.collection('users').doc('user1');
const docRef2 = firestore.collection('users').doc('user2');

batch.set(docRef1, {name: 'Alice'});
batch.update(docRef2, {age: 30});
batch.delete(firestore.collection('users').doc('user3'));

await batch.commit();
💻

Example

This example shows how to update two user documents and delete one user document in a single batch write. All changes happen together or not at all.

javascript
import { getFirestore, writeBatch, doc } from 'firebase/firestore';

async function batchWriteExample() {
  const firestore = getFirestore();
  const batch = writeBatch(firestore);

  const user1Ref = doc(firestore, 'users', 'user1');
  const user2Ref = doc(firestore, 'users', 'user2');
  const user3Ref = doc(firestore, 'users', 'user3');

  batch.set(user1Ref, { name: 'Alice', active: true });
  batch.update(user2Ref, { age: 28 });
  batch.delete(user3Ref);

  await batch.commit();
  console.log('Batch write completed successfully');
}

batchWriteExample();
Output
Batch write completed successfully
⚠️

Common Pitfalls

Common mistakes when using batch writes include:

  • Trying to commit an empty batch, which does nothing.
  • Using update on a document that does not exist, causing an error.
  • Exceeding Firestore's limit of 500 operations per batch.
  • Not awaiting batch.commit(), leading to unpredictable results.

Always check document existence before updating and keep batch size under 500 operations.

javascript
/* Wrong: update on non-existing doc causes error */
const batch = firestore.batch();
const docRef = firestore.collection('users').doc('nonexistent');
batch.update(docRef, { age: 25 });
await batch.commit(); // This will fail

/* Right: use set() to create or overwrite */
const batch2 = firestore.batch();
batch2.set(docRef, { age: 25 });
await batch2.commit(); // Works fine
📊

Quick Reference

MethodDescriptionNotes
writeBatch()Creates a new batch objectStart your batch with this
batch.set(docRef, data)Sets data on a document, creates if missingOverwrites existing data
batch.update(docRef, data)Updates fields on an existing documentFails if document does not exist
batch.delete(docRef)Deletes a documentRemoves document from collection
batch.commit()Sends all operations atomicallyReturns a Promise

Key Takeaways

Use Firestore's writeBatch() to group multiple writes into one atomic operation.
Add set, update, or delete operations to the batch before committing.
Commit the batch with batch.commit() and always await its completion.
Do not exceed 500 operations per batch to avoid errors.
Use set() to create or overwrite documents; update() requires the document to exist.