What if you could update your data without ever losing what you already saved?
Why Setting with merge option in Firebase? - Purpose & Use Cases
Imagine you have a big notebook where you write down details about your friends. One day, you want to add a new phone number to a friend's page without erasing the other details you already wrote. But if you just write the new number on a fresh page, you lose all the old notes.
Manually rewriting or updating data without a merge option means you risk deleting important information. It's slow because you have to copy all existing details every time you want to add or change just one thing. Mistakes happen easily, and you might lose data you didn't mean to remove.
The merge option lets you add or update only the parts you want, without touching the rest. It's like adding a sticky note to the notebook page instead of rewriting the whole page. This keeps your data safe and saves time.
db.collection('users').doc('alice').set({phone: '123-4567'})
db.collection('users').doc('alice').set({phone: '123-4567'}, {merge: true})
You can safely update parts of your data anytime without losing anything else, making your app smarter and more reliable.
When a user updates their profile picture but keeps their name and email the same, the merge option updates only the picture without erasing other profile details.
Manual updates risk overwriting and losing data.
Merge option updates only what you want, keeping the rest safe.
This makes data management easier, faster, and less error-prone.
Practice
merge: true option do when updating a Firestore document?Solution
Step 1: Understand Firestore update behavior
By default, setting data replaces the whole document, removing unspecified fields.Step 2: Role of
Usingmerge: truemerge: trueupdates only the fields you specify, keeping other fields intact.Final Answer:
It updates only the specified fields without deleting others. -> Option BQuick Check:
Merge option = partial update [OK]
- Thinking merge deletes the whole document
- Confusing merge with creating new documents
- Assuming merge prevents updates
Solution
Step 1: Identify correct method and options
Firestore'ssetmethod accepts an options object as second parameter for merge.Step 2: Check syntax correctness
The correct syntax isset(data, {merge: true}). Theupdatemethod does not accept merge option.Final Answer:
docRef.set({name: 'Alice'}, {merge: true}); -> Option CQuick Check:
set with {merge: true} is correct syntax [OK]
- Using update() with merge option
- Passing merge as a separate argument without braces
- Using merge=true instead of {merge: true}
{name: 'Bob', age: 30, city: 'NY'}, what will be the document content after running docRef.set({age: 31}, {merge: true});?Solution
Step 1: Understand existing document
The document has fields: name='Bob', age=30, city='NY'.Step 2: Effect of set with merge
Usingsetwith{merge: true}updates only the specified fieldageto 31, keeping others unchanged.Final Answer:
{name: 'Bob', age: 31, city: 'NY'} -> Option DQuick Check:
Merge updates age, keeps name and city [OK]
- Thinking merge replaces whole document
- Expecting duplicate keys in document
- Assuming fields not mentioned get deleted
docRef.set({score: 100}, merge=true); but it throws an error. What is the problem?Solution
Step 1: Check merge option syntax
The merge option must be passed as an object:{merge: true}, not asmerge=true.Step 2: Confirm set supports merge
Thesetmethod supports merge option correctly when passed as an object.Final Answer:
The merge option must be passed as an object: {merge: true}. -> Option AQuick Check:
Merge option syntax requires braces [OK]
- Passing merge as a boolean without braces
- Using update() with merge option
- Assuming merge disables updates
{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?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 nestedwinsfield without deletinglosses.Final Answer:
docRef.set({stats: {wins: 6}}, {mergeFields: ['stats.wins']}); -> Option AQuick Check:
mergeFields updates nested fields precisely [OK]
- Using merge true replaces whole nested object
- Not specifying mergeFields for nested updates
- Assuming update() always merges nested fields
Ready to prove it?
Pass the test and this pattern moves unknown
