Step 1: Identify why entire document is overwritten
Using set() without {merge: true} replaces the whole document, causing data loss.
Step 2: Fix the code to update only one field
Add {merge: true} to set() to update only the email field without overwriting others.
Final Answer:
Missing {merge: true} option in set() -> Option D
Quick Check:
Partial update needs merge true [OK]
Hint: Add {merge: true} to set() to avoid overwriting [OK]
Common Mistakes:
Using set() without merge option
Confusing update() and set() usage
Assuming field names cause overwrite
5. You want to reduce Firebase costs by minimizing writes when updating user profiles. Which approach is best to update multiple fields efficiently without overwriting existing data?
hard
A. Use multiple separate set() calls without merge option
B. Use a batch write with set() and merge: true for all updates
C. Use update() for each field separately in different calls
D. Overwrite entire document with set() each time
Solution
Step 1: Understand cost impact of writes
Each write operation costs money. Multiple separate writes increase cost and risk overwriting data.
Step 2: Choose efficient update method
Batch writes combine multiple updates into one commit, reducing network overhead. Using set() with {merge: true} updates only needed fields without overwriting.
Final Answer:
Use a batch write with set() and merge: true for all updates -> Option B
Quick Check:
Batch + merge = efficient partial updates [OK]
Hint: Batch writes with merge true save cost and data [OK]
Common Mistakes:
Overwriting entire documents unnecessarily
Using many separate update calls
Ignoring batch writes for multiple updates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.