0
0
Firebasecloud~10 mins

Updating specific fields in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Updating specific fields
Start
Select Document
Specify Fields to Update
Send Update Request
Database Applies Changes
Confirm Update Success
End
This flow shows how to update only certain fields in a Firebase document without changing the whole document.
Execution Sample
Firebase
docRef.update({
  age: 30,
  city: 'New York'
});
Updates only the 'age' and 'city' fields of the selected document.
Process Table
StepActionFields SentDatabase State BeforeDatabase State AfterResult
1Select document 'user123'N/A{name: 'Alice', age: 25, city: 'Boston'}{name: 'Alice', age: 25, city: 'Boston'}Document selected
2Call update() with {age: 30, city: 'New York'}{age: 30, city: 'New York'}{name: 'Alice', age: 25, city: 'Boston'}{name: 'Alice', age: 30, city: 'New York'}Fields updated
3Confirm update successN/A{name: 'Alice', age: 30, city: 'New York'}{name: 'Alice', age: 30, city: 'New York'}Update confirmed
💡 Update completes after specified fields are changed; other fields remain unchanged.
Status Tracker
VariableStartAfter Step 2Final
docRefReference to 'user123'Reference to 'user123'Reference to 'user123'
age253030
city'Boston''New York''New York'
name'Alice''Alice''Alice'
Key Moments - 2 Insights
Why does the 'name' field stay the same after update?
Because update() changes only the specified fields (age and city). Fields not listed, like 'name', stay unchanged as shown in execution_table row 2.
What happens if you try to update a field that does not exist yet?
Firebase creates the new field with the given value. This is like adding a new key to the document, similar to how age and city were updated in the example.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'city' after step 2?
A'Boston'
B'New York'
Cundefined
D'Chicago'
💡 Hint
Check the 'Database State After' column in row 2 of the execution_table.
At which step does the database state actually change?
AStep 2
BStep 1
CStep 3
DNo change occurs
💡 Hint
Look at the 'Database State After' column and see when fields are updated.
If you add a new field 'email' in the update call, what happens to the document?
AThe document is replaced entirely with only 'email'
BThe update fails because 'email' does not exist
COnly 'email' is added or updated, other fields stay the same
DAll fields are deleted except 'email'
💡 Hint
Refer to the key_moments explanation about adding new fields.
Concept Snapshot
Firebase update() changes only specified fields in a document.
Unspecified fields remain unchanged.
New fields can be added by including them in update().
update() does not replace the whole document.
Use update() to modify parts without overwriting all data.
Full Transcript
This visual execution shows how Firebase updates specific fields in a document. First, a document is selected. Then, update() is called with only the fields to change. The database changes those fields but keeps others intact. The update is confirmed successful. This method avoids overwriting the entire document and allows adding new fields easily.