0
0
Firebasecloud~20 mins

Updating specific fields in Firebase - Practice Problems & Coding Challenges

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