📖 Scenario: You are building a simple app that needs to add multiple user profiles to a Firestore database at once. To keep the data consistent and save time, you will use batch writes.
🎯 Goal: Create a Firestore batch write that adds three user documents with exact data in a single commit.
📋 What You'll Learn
Create a Firestore batch instance
Add three user documents with specified IDs and fields to the batch
Commit the batch to write all documents at once
💡 Why This Matters
🌍 Real World
Batch writes are used to perform multiple database changes at once, ensuring all succeed or fail together. This is useful for syncing related data or improving performance.
💼 Career
Understanding batch writes is important for backend and full-stack developers working with Firestore to maintain data consistency and optimize database operations.
Progress0 / 4 steps
1
Initialize Firestore and batch
Create a variable called db that initializes Firestore using getFirestore(). Then create a variable called batch that initializes a batch write using writeBatch(db).
Firebase
Hint
Use getFirestore() to get the Firestore instance and writeBatch(db) to start a batch.
2
Prepare user document references
Create three variables called userRef1, userRef2, and userRef3. Each should use doc(db, 'users', 'user1'), doc(db, 'users', 'user2'), and doc(db, 'users', 'user3') respectively to reference documents in the 'users' collection.
Firebase
Hint
Use doc(db, 'users', 'userX') to get document references for each user.
3
Add user data to batch
Use batch.set() to add three user documents to the batch. For userRef1, set {name: 'Alice', age: 30}. For userRef2, set {name: 'Bob', age: 25}. For userRef3, set {name: 'Charlie', age: 35}.
Firebase
Hint
Use batch.set(documentRef, data) to add each user document to the batch.
4
Commit the batch write
Add a line to commit the batch write using await batch.commit(). Make sure the function is async or use a top-level await if supported.
Firebase
Hint
Use await batch.commit() to send all writes to Firestore at once.
Practice
(1/5)
1. What is the main purpose of using batch writes in Firebase?
easy
A. To group multiple data changes into a single request
B. To read data faster from the database
C. To create multiple databases at once
D. To backup data automatically
Solution
Step 1: Understand batch writes concept
Batch writes allow grouping many data changes into one request to keep data consistent.
Step 2: Identify the main purpose
The main purpose is to combine multiple changes to save time and ensure consistency.
Final Answer:
To group multiple data changes into a single request -> Option A
Quick Check:
Batch writes = group changes [OK]
Hint: Batch writes combine changes to save time and keep data consistent [OK]
Common Mistakes:
Thinking batch writes speed up data reading
Confusing batch writes with database creation
Assuming batch writes automatically backup data
2. Which of the following is the correct way to start a batch write in Firebase?
easy
A. const batch = firestore.batch();
B. const batch = firestore.startBatch();
C. const batch = firestore.createBatchWrite();
D. const batch = firestore.newBatch();
Solution
Step 1: Recall Firebase batch write syntax
The correct method to start a batch is firestore.batch().
Step 2: Compare options
Only const batch = firestore.batch(); uses the exact Firebase API method firestore.batch().
Final Answer:
const batch = firestore.batch(); -> Option A
Quick Check:
Start batch with firestore.batch() [OK]
Hint: Use firestore.batch() to start batch writes [OK]
Common Mistakes:
Using non-existent methods like startBatch or newBatch
Confusing batch creation with other Firestore methods
Misspelling the batch method name
3. What will happen if you run this Firebase batch write code?