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
Step 1: Recall update() requirements
update() requires the document to exist; otherwise, it throws an error.
Step 2: Check dot notation support
Dot notation is supported for nested fields in update(), so that is not the cause.
Step 3: Confirm field name validity
Field names with dot notation are valid in Firestore for nested updates.
Final Answer:
The document does not exist, so update() fails. -> Option B
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 });