0
0
Firebasecloud~10 mins

Setting with merge option in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Setting with merge option
Start: Document exists or not
Call set() with merge option
Check merge flag
Save changes
End
This flow shows how using the merge option in set() updates only specified fields without deleting others, unlike a full overwrite.
Execution Sample
Firebase
docRef.set({ age: 30 }, { merge: true });
Updates only the 'age' field in the document, keeping other fields intact.
Process Table
StepDocument State BeforeActionMerge OptionDocument State After
1{}Call set({ age: 30 })true{ age: 30 }
2{ name: 'Alice', city: 'NY' }Call set({ age: 30 })true{ name: 'Alice', city: 'NY', age: 30 }
3{ name: 'Alice', city: 'NY' }Call set({ age: 30 })false{ age: 30 }
4{ age: 30 }Call set({ city: 'LA' })true{ age: 30, city: 'LA' }
5{ age: 30, city: 'LA' }Call set({ city: 'LA' })false{ city: 'LA' }
6{ city: 'LA' }Call set({ })true{ city: 'LA' }
7{ city: 'LA' }Call set({ })false{}
💡 Execution ends after all set calls demonstrate merge vs overwrite effects.
Status Tracker
Document StateStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7
doc{}{ age: 30 }{ name: 'Alice', city: 'NY', age: 30 }{ age: 30 }{ age: 30, city: 'LA' }{ city: 'LA' }{ city: 'LA' }{}
Key Moments - 2 Insights
Why does the document keep other fields when merge is true but loses them when merge is false?
When merge is true (see steps 2 and 4), only specified fields are updated or added, preserving others. When false (steps 3 and 5), the entire document is replaced with new data, removing unspecified fields.
What happens if you call set() with an empty object and merge true or false?
With merge true (step 6), the document stays unchanged because no fields are added or removed. With merge false (step 7), the document is overwritten and becomes empty.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the document state after step 2?
A{ name: 'Alice', city: 'NY', age: 30 }
B{ name: 'Alice', city: 'NY' }
C{ age: 30 }
D{}
💡 Hint
Check the 'Document State After' column for step 2 in the execution_table.
At which step does the document lose all fields and become empty?
AStep 5
BStep 6
CStep 7
DStep 3
💡 Hint
Look for '{}' in the 'Document State After' column in the execution_table.
If merge option was false at step 4, what would the document state be after that step?
A{ age: 30, city: 'LA' }
B{ city: 'LA' }
C{ age: 30 }
D{}
💡 Hint
Compare step 4 with merge true vs false in the execution_table.
Concept Snapshot
set(data, {merge: true}) updates only specified fields.
Without merge, set() overwrites entire document.
Merge preserves existing fields not in new data.
Empty data with merge true keeps document unchanged.
Empty data with merge false clears document.
Full Transcript
This visual execution shows how Firebase's set() method behaves with the merge option. When merge is true, only the fields provided are updated or added, and existing fields remain untouched. When merge is false or omitted, the entire document is replaced by the new data, removing any fields not included. For example, setting { age: 30 } with merge true adds or updates the age field but keeps other fields like name or city. Setting with merge false replaces the whole document with only the age field. Calling set() with an empty object and merge true leaves the document unchanged, but with merge false clears it. This helps understand how to safely update parts of a document without losing data.