0
0
Firebasecloud~10 mins

Writing data (set, update, push) in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Writing data (set, update, push)
Start
Choose write method
set()
Overwrite data
Confirm write success
End
This flow shows choosing between set, update, and push to write data in Firebase, then performing the write and confirming success.
Execution Sample
Firebase
ref.set({name: 'Alice'});
ref.update({age: 30});
ref.push({score: 100});
This code writes data by overwriting, updating fields, and adding a new child in Firebase.
Process Table
StepActionData SentEffect on DatabaseResult
1Call set(){name: 'Alice'}Overwrite data at ref with {name: 'Alice'}Data at ref is now {name: 'Alice'}
2Call update(){age: 30}Add/modify 'age' field, keep other fieldsData at ref is now {name: 'Alice', age: 30}
3Call push(){score: 100}Add new child with unique key under refNew child created with {score: 100}
4Confirm writesN/AAll writes succeededDatabase updated successfully
💡 All write operations completed successfully with expected data changes.
Status Tracker
VariableStartAfter set()After update()After push()
Database at ref{}{name: 'Alice'}{name: 'Alice', age: 30}{name: 'Alice', age: 30, newChildKey: {score: 100}}
Key Moments - 3 Insights
Why does set() overwrite all data at the reference?
Because set() replaces the entire data at the reference, as shown in step 1 of the execution_table where previous data is replaced by {name: 'Alice'}.
How does update() differ from set() in modifying data?
update() only changes specified fields without removing others, as seen in step 2 where 'age' is added but 'name' remains.
What does push() do differently compared to set() and update()?
push() creates a new child with a unique key under the reference, adding data without overwriting existing data, shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the database content after step 2?
A{name: 'Alice', age: 30}
B{age: 30}
C{name: 'Alice'}
D{}
💡 Hint
Check the 'Effect on Database' and 'Result' columns for step 2 in the execution_table.
At which step does the database get a new child with a unique key?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the action 'Call push()' in the execution_table.
If you want to replace all data at a reference, which method should you use?
Aupdate()
Bset()
Cpush()
DNone of these
💡 Hint
Refer to the explanation of set() in the key_moments and execution_table step 1.
Concept Snapshot
Firebase Writing Data:
- set(): Overwrites all data at a reference.
- update(): Modifies specified fields, keeps others.
- push(): Adds new child with unique key.
Use set() to replace, update() to change fields, push() to add entries.
Full Transcript
This lesson shows how to write data in Firebase using set, update, and push methods. First, set() replaces all data at a location. Then, update() changes or adds specific fields without removing others. Finally, push() adds a new child with a unique key under the reference. Each step changes the database state as shown. Understanding these differences helps manage data safely and effectively in Firebase.