You have a Firestore document with fields {name: 'Alice', age: 30}. You run the following code to update the document:
docRef.set({age: 31}, {merge: true});What will be the final state of the document?
docRef.set({age: 31}, {merge: true});
Think about what the merge: true option does when updating a document.
The merge: true option updates only the specified fields and keeps the others intact. So name remains and age is updated to 31.
You want to update only the city field inside the address object of a Firestore document without overwriting the entire address object. Which code snippet correctly does this?
Consider how to merge only a nested field inside an object.
Using mergeFields: ['address.city'] updates only the nested city field inside address without overwriting other fields in address.
You are designing a Firestore-based app where users can update their profiles. Some updates only change one or two fields, but others replace the entire profile. Which approach is best to minimize data loss and optimize performance?
Think about when you want to keep existing data and when you want to replace it.
Using merge for partial updates prevents data loss. For full replacements, overwriting is simpler and clearer. Combining both approaches fits different update needs.
Consider Firestore security rules that allow users to update only their own profile fields. What risk might arise if you use set() with {merge: true} carelessly?
Think about how security rules validate nested fields during merges.
If security rules do not validate nested fields carefully, users might update fields they shouldn't by merging partial data.
You want to update multiple fields in a Firestore document but only want to send minimal data over the network and avoid overwriting unintended fields. Which approach is best?
Consider how to minimize data sent and avoid accidental overwrites.
Using mergeFields lets you specify exactly which fields to update, sending minimal data and protecting other fields from changes.