Setting with merge option in Firebase - Time & Space Complexity
When we update data in Firebase using the merge option, it changes only parts of the data instead of replacing everything.
We want to know how the time it takes grows as the data size grows.
Analyze the time complexity of this Firebase update with merge.
const docRef = firestore.collection('users').doc('user123');
const updateData = { age: 30, city: 'New York' };
await docRef.set(updateData, { merge: true });
This updates only the fields given, keeping other fields unchanged in the document.
Look at what happens when we run this update.
- Primary operation: One network call to update the document with merge.
- How many times: Exactly once per update call.
As the number of fields in updateData grows, the update sends more data but still only one call.
| Input Size (fields to update) | Approx. API Calls/Operations |
|---|---|
| 10 | 1 network call with 10 fields |
| 100 | 1 network call with 100 fields |
| 1000 | 1 network call with 1000 fields |
Pattern observation: The number of calls stays the same, but the data size sent grows with the number of fields.
Time Complexity: O(n)
This means the time grows linearly with the number of fields you update in the merge.
[X] Wrong: "Using merge means the update time is always constant no matter how many fields I update."
[OK] Correct: Even though there is only one update call, the amount of data sent grows with the number of fields, so time grows too.
Understanding how partial updates scale helps you design efficient data updates and shows you know how cloud calls behave as data grows.
"What if we replaced the merge option with a full set without merge? How would the time complexity change?"
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
