0
0
Firebasecloud~20 mins

Setting with merge option in Firebase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Firestore Merge Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
Effect of merge option on Firestore document update

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?

Firebase
docRef.set({age: 31}, {merge: true});
A{name: 'Alice', age: 30, age: 31}
B{name: 'Alice', age: 31}
C{name: 'Alice'}
D{age: 31}
Attempts:
2 left
💡 Hint

Think about what the merge: true option does when updating a document.

Configuration
intermediate
2:00remaining
Using merge option with nested fields in Firestore

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?

AdocRef.set({address: {city: 'New York'}}, {mergeFields: ['city']});
BdocRef.set({address: {city: 'New York'}}, {merge: true});
CdocRef.update({address: {city: 'New York'}});
DdocRef.set({address: {city: 'New York'}}, {mergeFields: ['address.city']});
Attempts:
2 left
💡 Hint

Consider how to merge only a nested field inside an object.

Architecture
advanced
2:00remaining
Choosing merge vs overwrite for Firestore document updates

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?

AAlways use <code>set()</code> with <code>{merge: true}</code> to update fields incrementally.
BAlways use <code>set()</code> without merge to overwrite the entire document.
CUse <code>set()</code> with <code>{merge: true}</code> for partial updates and <code>set()</code> without merge for full replacements.
DUse <code>update()</code> for all updates regardless of size.
Attempts:
2 left
💡 Hint

Think about when you want to keep existing data and when you want to replace it.

security
advanced
2:00remaining
Security implications of using merge option in Firestore writes

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?

AUsers might overwrite fields they are not allowed to change if rules do not check nested fields properly.
BMerge option disables all security rules for the document.
CMerge option causes Firestore to ignore write permissions.
DThere is no security risk; merge only updates specified fields.
Attempts:
2 left
💡 Hint

Think about how security rules validate nested fields during merges.

Best Practice
expert
2:00remaining
Optimizing Firestore writes with merge and mergeFields options

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?

AUse <code>set()</code> with <code>{mergeFields: ['field1', 'field2']}</code> specifying only the fields to update.
BUse <code>update()</code> with all fields to update.
CUse <code>set()</code> without merge and send the entire document data.
DUse <code>set()</code> with <code>{merge: true}</code> and include all fields to update in the data object.
Attempts:
2 left
💡 Hint

Consider how to minimize data sent and avoid accidental overwrites.