0
0
Firebasecloud~10 mins

Adding documents (add vs set) in Firebase - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Process Flow - Adding documents (add vs set)
Start
Choose collection
Auto-generate ID
Save data
Document saved
End
You start by choosing a collection. Using add creates a new document with an auto ID. Using set saves data to a document with a specified ID.
Execution Sample
Firebase
const docRef = await collectionRef.add({name: 'Alice'});
// vs
await collectionRef.doc('user123').set({name: 'Bob'});
Shows adding a document with auto ID vs setting a document with a specific ID.
Process Table
StepMethodDocument IDActionResult
1addauto-generatedCreate new doc with auto IDDocument created with new ID
2set'user123'Create or overwrite doc with ID 'user123'Document created or replaced
3addauto-generatedAdd another doc with auto IDNew document created with different ID
4set'user123'Overwrite doc with ID 'user123'Document data replaced
5end--No more actions
💡 No more document operations, process ends.
Status Tracker
VariableStartAfter 1After 2After 3After 4Final
docRef.idundefinedauto-generated ID 1auto-generated ID 1auto-generated ID 1auto-generated ID 1auto-generated ID 1
collectionRef.docsempty1 doc (auto ID 1)2 docs (auto ID 1, 'user123')3 docs (auto ID 1, 'user123', auto ID 2)3 docs (auto ID 1, 'user123' updated, auto ID 2)3 docs (final state)
Key Moments - 2 Insights
Why does add create a new document each time but set can overwrite?
Because add auto-generates a unique ID for each document (see step 1 and 3), while set uses the specified ID and replaces data if the document exists (see step 2 and 4).
Can set create a document if it doesn't exist?
Yes, set creates the document if it does not exist, as shown in step 2 where 'user123' is created.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the document ID created by the first add method?
Aauto-generated ID 1
B'user123'
Cauto-generated ID 2
Dundefined
💡 Hint
Check row 1 under Document ID in the execution_table.
At which step does the document with ID 'user123' get overwritten?
AStep 1
BStep 2
CStep 4
DStep 3
💡 Hint
Look at the Action and Result columns for 'user123' in the execution_table.
If you want to add multiple documents without specifying IDs, which method should you use?
Aset
Badd
Cdoc
Dupdate
💡 Hint
Refer to the concept_flow where add auto-generates IDs for new documents.
Concept Snapshot
add(): Adds a new document with an auto-generated ID.
set(): Creates or overwrites a document with a specified ID.
Use add for quick inserts without ID control.
Use set to control document IDs or overwrite.
add returns a reference to the new doc.
set returns a promise after write completes.
Full Transcript
This visual execution shows how Firebase Firestore adds documents using add and set methods. First, add creates a new document with an auto-generated ID, ensuring unique documents each time. Then, set creates or overwrites a document with a specified ID, allowing control over document keys. The execution table traces each step, showing document IDs and actions. Variable tracking shows how document references and collection contents change. Key moments clarify why add always creates new docs and set can overwrite. The quiz tests understanding of document IDs and method choices. The snapshot summarizes usage rules for add and set.