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?
const docRef = firestore.collection('users').doc('user123'); // Which update command is correct?
Think about how to update only a nested field without replacing the whole object.
Using dot notation like 'profile.age' updates only the nested age field inside profile. Option B replaces the entire profile object, losing other fields. Option B overwrites the whole document. Option B tries to set profile to a number, which is incorrect.
You want to update a Firestore document by adding new fields but keep all existing fields intact.
Which command achieves this behavior?
const docRef = firestore.collection('settings').doc('config'); // Choose the correct update command
Consider how to merge new data with existing document data.
Using set with { merge: true } merges the new fields with existing ones without deleting any. Option D overwrites the whole document. Option D updates but fails if the document doesn't exist. Option D merges only specified fields but is more complex and less common.
You have a Firestore document with a likes counter. Multiple users can like simultaneously.
Which approach ensures the counter updates correctly without losing increments?
Think about atomic operations that Firestore provides.
Firestore's FieldValue.increment(1) atomically increments the counter, preventing race conditions. Option C can cause lost updates if multiple clients read and write simultaneously. Option C overwrites the whole document and risks data loss. Option C works but is more complex and less efficient than atomic increments.
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?
match /users/{userId} {
allow update: if request.auth.uid == userId && ... ;
}Check how to detect which fields are changed in the update.
Option A uses diff to check that only the status field is changed. Option A restricts the entire document to only have status, disallowing other fields. Option A checks if status is unchanged, which is opposite of the goal. Option A only checks type, not field changes.
You need to update several nested fields inside a Firestore document atomically, ensuring no partial updates happen.
Which approach is best?
Consider how Firestore handles atomic updates in a single call.
Using a single update call with dot notation updates all specified nested fields atomically. Multiple update calls are not atomic and can cause partial updates. Using set with merge multiple times is inefficient and not atomic. Reading and then setting without merge overwrites the whole document, risking data loss.