Bird
Raised Fist0
Firebasecloud~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the merge: true option do when updating a Firestore document?
easy
A. It deletes the entire document before updating.
B. It updates only the specified fields without deleting others.
C. It creates a new document with the same ID.
D. It prevents any updates to the document.

Solution

  1. Step 1: Understand Firestore update behavior

    By default, setting data replaces the whole document, removing unspecified fields.
  2. Step 2: Role of merge: true

    Using merge: true updates only the fields you specify, keeping other fields intact.
  3. Final Answer:

    It updates only the specified fields without deleting others. -> Option B
  4. Quick Check:

    Merge option = partial update [OK]
Hint: Merge means update fields, keep others safe [OK]
Common Mistakes:
  • Thinking merge deletes the whole document
  • Confusing merge with creating new documents
  • Assuming merge prevents updates
2. Which of the following is the correct syntax to update a Firestore document with merge option in JavaScript?
easy
A. docRef.set({name: 'Alice'}, merge=true);
B. docRef.update({name: 'Alice'}, {merge: true});
C. docRef.set({name: 'Alice'}, {merge: true});
D. docRef.update({name: 'Alice'}, merge=true);

Solution

  1. Step 1: Identify correct method and options

    Firestore's set method accepts an options object as second parameter for merge.
  2. Step 2: Check syntax correctness

    The correct syntax is set(data, {merge: true}). The update method does not accept merge option.
  3. Final Answer:

    docRef.set({name: 'Alice'}, {merge: true}); -> Option C
  4. Quick Check:

    set with {merge: true} is correct syntax [OK]
Hint: Use set() with {merge: true}, not update() [OK]
Common Mistakes:
  • Using update() with merge option
  • Passing merge as a separate argument without braces
  • Using merge=true instead of {merge: true}
3. Given the Firestore document data: {name: 'Bob', age: 30, city: 'NY'}, what will be the document content after running docRef.set({age: 31}, {merge: true});?
medium
A. {name: 'Bob', age: 30, city: 'NY', age: 31}
B. {age: 31}
C. {name: 'Bob', city: 'NY'}
D. {name: 'Bob', age: 31, city: 'NY'}

Solution

  1. Step 1: Understand existing document

    The document has fields: name='Bob', age=30, city='NY'.
  2. Step 2: Effect of set with merge

    Using set with {merge: true} updates only the specified field age to 31, keeping others unchanged.
  3. Final Answer:

    {name: 'Bob', age: 31, city: 'NY'} -> Option D
  4. Quick Check:

    Merge updates age, keeps name and city [OK]
Hint: Merge updates given fields, keeps rest intact [OK]
Common Mistakes:
  • Thinking merge replaces whole document
  • Expecting duplicate keys in document
  • Assuming fields not mentioned get deleted
4. You try to update a Firestore document with docRef.set({score: 100}, merge=true); but it throws an error. What is the problem?
medium
A. The merge option must be passed as an object: {merge: true}.
B. The set method does not support merge option.
C. The score field cannot be updated.
D. You must use update() instead of set() for merge.

Solution

  1. Step 1: Check merge option syntax

    The merge option must be passed as an object: {merge: true}, not as merge=true.
  2. Step 2: Confirm set supports merge

    The set method supports merge option correctly when passed as an object.
  3. Final Answer:

    The merge option must be passed as an object: {merge: true}. -> Option A
  4. Quick Check:

    Merge option syntax requires braces [OK]
Hint: Pass merge as {merge: true}, not merge=true [OK]
Common Mistakes:
  • Passing merge as a boolean without braces
  • Using update() with merge option
  • Assuming merge disables updates
5. You have a Firestore document {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?
hard
A. docRef.set({stats: {wins: 6}}, {mergeFields: ['stats.wins']});
B. docRef.set({stats: {wins: 6, losses: 2}}, {merge: true});
C. docRef.set({stats: {wins: 6}}, {merge: true});
D. docRef.update({stats: {wins: 6}});

Solution

  1. Step 1: Understand nested field update with merge

    Using {merge: true} with nested objects replaces the whole nested object, removing unspecified keys.
  2. Step 2: Use mergeFields to update nested field only

    Passing {mergeFields: ['stats.wins']} updates only the nested wins field without deleting losses.
  3. Final Answer:

    docRef.set({stats: {wins: 6}}, {mergeFields: ['stats.wins']}); -> Option A
  4. Quick Check:

    mergeFields updates nested fields precisely [OK]
Hint: Use mergeFields to update nested fields safely [OK]
Common Mistakes:
  • Using merge true replaces whole nested object
  • Not specifying mergeFields for nested updates
  • Assuming update() always merges nested fields
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard