Firebase Firestore: Setting Document Data with Merge Option
📖 Scenario: You are building a simple app that stores user profiles in Firebase Firestore. Sometimes you want to update only part of a user's profile without overwriting the entire document.
🎯 Goal: Learn how to use the set method with the merge: true option to update parts of a Firestore document safely.
📋 What You'll Learn
Create a Firestore document reference for a user with ID 'user123'.
Create an initial user profile object with name and age.
Use set to save the initial profile to Firestore.
Create an update object with a new age and city.
Use set with { merge: true } to update the user profile without deleting existing fields.
💡 Why This Matters
🌍 Real World
Updating user profiles or settings in Firestore without deleting existing data is common in apps.
💼 Career
Understanding how to safely update Firestore documents is essential for backend and full-stack developers working with Firebase.
Progress0 / 4 steps
1
Create Firestore document reference and initial user profile
Create a variable called userRef that references the Firestore document at users/user123. Then create a variable called initialProfile with the exact object { name: 'Alice', age: 30 }.
Firebase
Hint
Use firestore.collection('users').doc('user123') to get the document reference.
2
Save the initial profile to Firestore
Use userRef.set(initialProfile) to save the initial profile object to Firestore.
Firebase
Hint
Call set on userRef with initialProfile as argument.
3
Create an update object with new age and city
Create a variable called updateProfile with the exact object { age: 31, city: 'New York' }.
Firebase
Hint
Define updateProfile with the new age and city fields.
4
Update the user profile using set with merge option
Use userRef.set(updateProfile, { merge: true }) to update the Firestore document without overwriting the entire profile.
Firebase
Hint
Call set on userRef with updateProfile and the option { merge: true }.
Practice
(1/5)
1. What does the merge: true option do when updating a Firestore document?
easy
A. It deletes the entire document before updating.
B. It updates only the specified fields without deleting others.
C. It creates a new document with the same ID.
D. It prevents any updates to the document.
Solution
Step 1: Understand Firestore update behavior
By default, setting data replaces the whole document, removing unspecified fields.
Step 2: Role of merge: true
Using merge: true updates only the fields you specify, keeping other fields intact.
Final Answer:
It updates only the specified fields without deleting others. -> Option B
Quick Check:
Merge option = partial update [OK]
Hint: Merge means update fields, keep others safe [OK]
Common Mistakes:
Thinking merge deletes the whole document
Confusing merge with creating new documents
Assuming merge prevents updates
2. Which of the following is the correct syntax to update a Firestore document with merge option in JavaScript?
easy
A. docRef.set({name: 'Alice'}, merge=true);
B. docRef.update({name: 'Alice'}, {merge: true});
C. docRef.set({name: 'Alice'}, {merge: true});
D. docRef.update({name: 'Alice'}, merge=true);
Solution
Step 1: Identify correct method and options
Firestore's set method accepts an options object as second parameter for merge.
Step 2: Check syntax correctness
The correct syntax is set(data, {merge: true}). The update method does not accept merge option.
Final Answer:
docRef.set({name: 'Alice'}, {merge: true}); -> Option C
Quick Check:
set with {merge: true} is correct syntax [OK]
Hint: Use set() with {merge: true}, not update() [OK]
Common Mistakes:
Using update() with merge option
Passing merge as a separate argument without braces
Using merge=true instead of {merge: true}
3. Given the Firestore document data: {name: 'Bob', age: 30, city: 'NY'}, what will be the document content after running docRef.set({age: 31}, {merge: true});?
medium
A. {name: 'Bob', age: 30, city: 'NY', age: 31}
B. {age: 31}
C. {name: 'Bob', city: 'NY'}
D. {name: 'Bob', age: 31, city: 'NY'}
Solution
Step 1: Understand existing document
The document has fields: name='Bob', age=30, city='NY'.
Step 2: Effect of set with merge
Using set with {merge: true} updates only the specified field age to 31, keeping others unchanged.
Final Answer:
{name: 'Bob', age: 31, city: 'NY'} -> Option D
Quick Check:
Merge updates age, keeps name and city [OK]
Hint: Merge updates given fields, keeps rest intact [OK]
Common Mistakes:
Thinking merge replaces whole document
Expecting duplicate keys in document
Assuming fields not mentioned get deleted
4. You try to update a Firestore document with docRef.set({score: 100}, merge=true); but it throws an error. What is the problem?
medium
A. The merge option must be passed as an object: {merge: true}.
B. The set method does not support merge option.
C. The score field cannot be updated.
D. You must use update() instead of set() for merge.
Solution
Step 1: Check merge option syntax
The merge option must be passed as an object: {merge: true}, not as merge=true.
Step 2: Confirm set supports merge
The set method supports merge option correctly when passed as an object.
Final Answer:
The merge option must be passed as an object: {merge: true}. -> Option A
Quick Check:
Merge option syntax requires braces [OK]
Hint: Pass merge as {merge: true}, not merge=true [OK]
Common Mistakes:
Passing merge as a boolean without braces
Using update() with merge option
Assuming merge disables updates
5. You have a Firestore document {name: 'Eve', stats: {wins: 5, losses: 2}}. You want to update only wins to 6 without removing losses. Which code correctly updates the document using merge?
hard
A. docRef.set({stats: {wins: 6}}, {mergeFields: ['stats.wins']});
B. docRef.set({stats: {wins: 6, losses: 2}}, {merge: true});
C. docRef.set({stats: {wins: 6}}, {merge: true});
D. docRef.update({stats: {wins: 6}});
Solution
Step 1: Understand nested field update with merge
Using {merge: true} with nested objects replaces the whole nested object, removing unspecified keys.
Step 2: Use mergeFields to update nested field only
Passing {mergeFields: ['stats.wins']} updates only the nested wins field without deleting losses.
Final Answer:
docRef.set({stats: {wins: 6}}, {mergeFields: ['stats.wins']}); -> Option A
Quick Check:
mergeFields updates nested fields precisely [OK]
Hint: Use mergeFields to update nested fields safely [OK]
Common Mistakes:
Using merge true replaces whole nested object
Not specifying mergeFields for nested updates
Assuming update() always merges nested fields
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.