Bird
Raised Fist0
Firebasecloud~20 mins

Updating specific fields in Firebase - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Firestore Update Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
What happens when you update a nested field in Firestore?

You have a Firestore document with a nested object field profile. You want to update only the age inside profile without overwriting the entire profile object.

Which update command will correctly update only age?

Firebase
const docRef = firestore.collection('users').doc('user123');

// Which update command is correct?
AdocRef.update({ profile: { age: 30 } });
BdocRef.update({ 'profile.age': 30 });
CdocRef.set({ profile: { age: 30 } });
DdocRef.update({ 'profile': 30 });
Attempts:
2 left
💡 Hint

Think about how to update only a nested field without replacing the whole object.

❓ Configuration
intermediate
2:00remaining
Which Firestore update command merges new fields without deleting existing ones?

You want to update a Firestore document by adding new fields but keep all existing fields intact.

Which command achieves this behavior?

Firebase
const docRef = firestore.collection('settings').doc('config');

// Choose the correct update command
AdocRef.set({ theme: 'dark' });
BdocRef.set({ theme: 'dark' }, { mergeFields: ['theme'] });
CdocRef.update({ theme: 'dark' });
DdocRef.set({ theme: 'dark' }, { merge: true });
Attempts:
2 left
💡 Hint

Consider how to merge new data with existing document data.

❓ Architecture
advanced
2:30remaining
How to design Firestore updates to avoid race conditions on counters?

You have a Firestore document with a likes counter. Multiple users can like simultaneously.

Which approach ensures the counter updates correctly without losing increments?

ARead the current <code>likes</code> value, add 1, then update the document with the new value.
BReplace the entire document with a new object containing <code>likes</code> incremented by 1.
CUse Firestore's atomic <code>FieldValue.increment(1)</code> to update the <code>likes</code> field.
DUse a transaction to read, increment, and write back the <code>likes</code> value.
Attempts:
2 left
💡 Hint

Think about atomic operations that Firestore provides.

❓ security
advanced
2:30remaining
Which Firestore security rule allows updating only specific fields?

You want to write a Firestore security rule that permits users to update only the status field of their own documents, but not other fields.

Which rule correctly enforces this?

Firebase
match /users/{userId} {
  allow update: if request.auth.uid == userId && ... ;
}
Arequest.resource.data.diff(resource.data).affectedKeys().hasOnly(['status'])
Brequest.resource.data.status == resource.data.status
Crequest.resource.data.status is string
Drequest.resource.data.keys().hasOnly(['status'])
Attempts:
2 left
💡 Hint

Check how to detect which fields are changed in the update.

✅ Best Practice
expert
3:00remaining
What is the best practice to update multiple nested fields atomically in Firestore?

You need to update several nested fields inside a Firestore document atomically, ensuring no partial updates happen.

Which approach is best?

AUse a single <code>update</code> call with dot notation for all nested fields to update at once.
BUse multiple <code>update</code> calls one after another for each nested field.
CUse <code>set</code> with <code>{ merge: true }</code> for each nested field separately.
DRead the document, modify nested fields locally, then use <code>set</code> without merge.
Attempts:
2 left
💡 Hint

Consider how Firestore handles atomic updates in a single call.

Practice

(1/5)
1. What does the update() method do in Firebase Firestore?
easy
A. It creates a new collection in Firestore.
B. It deletes the entire document from the collection.
C. It changes only specific fields in a document without overwriting the whole document.
D. It reads all documents in a collection.

Solution

  1. Step 1: Understand the purpose of update()

    The update() method modifies only the fields you specify in an existing document.
  2. Step 2: Compare with other methods

    Unlike set(), update() does not overwrite the entire document, preserving other fields.
  3. Final Answer:

    It changes only specific fields in a document without overwriting the whole document. -> Option C
  4. Quick Check:

    update() modifies specific fields only [OK]
Hint: update() changes fields without replacing the whole document [OK]
Common Mistakes:
  • Thinking update() deletes documents
  • Confusing update() with set() creating collections
  • Believing update() reads data
2. Which of the following is the correct syntax to update the field age to 30 in a Firestore document reference docRef?
easy
A. docRef.update({ age: 30 });
B. docRef.set({ age: 30 });
C. docRef.update(age = 30);
D. docRef.modify({ age: 30 });

Solution

  1. Step 1: Identify the correct method for updating fields

    The update() method is used to change specific fields without overwriting the document.
  2. Step 2: Check syntax correctness

    The correct syntax is docRef.update({ age: 30 }); where the fields to update are passed as an object.
  3. Final Answer:

    docRef.update({ age: 30 }); -> Option A
  4. Quick Check:

    update() uses object with fields [OK]
Hint: Use update() with an object to change fields [OK]
Common Mistakes:
  • Using set() which overwrites the document
  • Using invalid syntax like update(age = 30)
  • Using non-existent method modify()
3. Given this code snippet:
const docRef = firestore.collection('users').doc('user123');
docRef.update({ 'profile.name': 'Alice', 'profile.age': 28 });

What will happen if the document user123 does not exist?
medium
A. The update will fail with an error because the document does not exist.
B. The update will silently do nothing and return success.
C. The update will create the document with the specified fields.
D. The update will overwrite the entire document with the new fields.

Solution

  1. Step 1: Understand update() behavior on non-existing documents

    The update() method requires the document to exist; otherwise, it throws an error.
  2. Step 2: Analyze the code context

    Since user123 does not exist, the update call will fail with an error.
  3. Final Answer:

    The update will fail with an error because the document does not exist. -> Option A
  4. Quick Check:

    update() fails if document missing [OK]
Hint: update() needs existing document; else error occurs [OK]
Common Mistakes:
  • Assuming update() creates documents
  • Thinking update() silently succeeds
  • Confusing update() with set() with merge
4. You try to update a nested field using this code:
docRef.update({ 'address.city': 'New York' });

But you get an error. What is the most likely cause?
medium
A. Dot notation is not supported for nested fields in update().
B. The document does not exist, so update() fails.
C. You must use set() instead of update() for nested fields.
D. The field name 'address.city' is invalid in Firestore.

Solution

  1. Step 1: Recall update() requirements

    update() requires the document to exist; otherwise, it throws an error.
  2. Step 2: Check dot notation support

    Dot notation is supported for nested fields in update(), so that is not the cause.
  3. Step 3: Confirm field name validity

    Field names with dot notation are valid in Firestore for nested updates.
  4. Final Answer:

    The document does not exist, so update() fails. -> Option B
  5. Quick Check:

    update() fails if document missing, dot notation allowed [OK]
Hint: update() fails if document missing; dot notation is valid [OK]
Common Mistakes:
  • Believing dot notation is unsupported
  • Thinking set() is required for nested fields
  • Assuming field names with dots are invalid
5. You want to update the nested field settings.notifications.email to false in a Firestore document. The document may or may not exist. Which code snippet correctly updates or creates the document without overwriting other fields?
hard
A. docRef.update({ 'settings.notifications.email': false });
B. docRef.set({ 'settings.notifications.email': false });
C. docRef.update({ settings: { notifications: { email: false } } });
D. docRef.set({ 'settings.notifications.email': false }, { merge: true });

Solution

  1. Step 1: Understand update() vs set() with merge

    update() fails if the document does not exist. set() with { merge: true } creates or updates without overwriting.
  2. Step 2: Analyze each option

    docRef.update({ 'settings.notifications.email': false }); fails if document missing. docRef.set({ 'settings.notifications.email': false }); overwrites entire document. docRef.set({ 'settings.notifications.email': false }, { merge: true }); correctly merges nested field. docRef.update({ settings: { notifications: { email: false } } }); incorrectly uses update() with nested object, which overwrites nested fields.
  3. Final Answer:

    docRef.set({ 'settings.notifications.email': false }, { merge: true }); -> Option D
  4. Quick Check:

    Use set() with merge:true to update or create safely [OK]
Hint: Use set() with merge:true to update nested fields safely [OK]
Common Mistakes:
  • Using update() when document may not exist
  • Using set() without merge, overwriting data
  • Passing nested objects to update() causing overwrite
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