📖 Scenario: You are managing a small app's user data stored in Firebase Firestore. You want to update only certain fields of a user's document without changing the entire document.
🎯 Goal: Learn how to update specific fields in a Firestore document using Firebase SDK.
📋 What You'll Learn
Create a Firestore document with initial user data
Add a configuration variable for the user ID
Write code to update specific fields in the Firestore document
Complete the update operation with proper Firestore method
💡 Why This Matters
🌍 Real World
Updating specific fields in Firestore is common when you want to change user profile details without affecting other data.
💼 Career
Knowing how to update fields in Firestore is essential for frontend and backend developers working with Firebase apps.
Progress0 / 4 steps
1
Create initial user data document
Create a Firestore document reference called userDoc pointing to the collection users and document ID user123. Then create an object called initialData with these exact fields: name set to "Alice", age set to 30, and city set to "New York".
Firebase
Hint
Use firestore.collection('users').doc('user123') to get the document reference.
2
Add user ID configuration variable
Create a constant variable called userId and set it to the string "user123".
Firebase
Hint
Define userId before using it in userDoc.
3
Write update data object
Create an object called updateData with these exact fields: age set to 31 and city set to "Boston". This object will hold the fields to update.
Firebase
Hint
Only include the fields you want to change in updateData.
4
Update specific fields in Firestore document
Use the update method on userDoc to update the document with the fields in updateData.
Firebase
Hint
Use userDoc.update(updateData) to change only the specified fields.
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
Step 1: Understand the purpose of update()
The update() method modifies only the fields you specify in an existing document.
Step 2: Compare with other methods
Unlike set(), update() does not overwrite the entire document, preserving other fields.
Final Answer:
It changes only specific fields in a document without overwriting the whole document. -> Option C
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
Step 1: Identify the correct method for updating fields
The update() method is used to change specific fields without overwriting the document.
Step 2: Check syntax correctness
The correct syntax is docRef.update({ age: 30 }); where the fields to update are passed as an object.
Final Answer:
docRef.update({ age: 30 }); -> Option A
Quick Check:
update() uses object with fields [OK]
Hint: Use update() with an object to change fields [OK]
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 });