Discover how a simple choice can save your data from being lost or overwritten!
Adding documents (add vs set) in Firebase - When to Use Which
Imagine you have a notebook where you write down important notes. You want to add a new note, but you have to decide whether to write it on a new page automatically or choose a specific page yourself.
Manually choosing pages or writing notes can be confusing and slow. You might accidentally overwrite an existing note or waste time finding a free page. This causes mistakes and frustration.
Using 'add' lets the system pick a new page for your note automatically, so you never overwrite anything. Using 'set' lets you choose exactly where to write, giving you control when you need it.
db.collection('notes').doc('note1').set({text: 'Hello'})
db.collection('notes').add({text: 'Hello'})
This choice lets you easily add new data safely or update specific data exactly as you want.
When saving user messages, 'add' quickly creates new entries without conflicts. When updating user profiles, 'set' updates the exact record you want.
'add' creates new documents with unique IDs automatically.
'set' writes data to a specific document ID you choose.
Choosing the right method prevents data loss and saves time.
Practice
add and set when adding documents in Firebase?Solution
Step 1: Understand
addbehavioraddcreates a new document with a unique ID generated by Firebase automatically.Step 2: Understand
setbehaviorsetcreates or updates a document at a specified ID you provide.Final Answer:
addcreates a document with an automatic unique ID,setuses a specified ID. -> Option AQuick Check:
addauto ID,setspecified ID = A [OK]
- Thinking set always creates new documents only
- Confusing which method generates IDs automatically
- Assuming add can update existing documents
Solution
Step 1: Identify method for automatic ID
addcreates a new document with an automatic ID inside a collection.Step 2: Check syntax correctness
db.collection('users').add({name: 'Alice'})is the correct syntax to add a new document with auto ID.Final Answer:
db.collection('users').add({name: 'Alice'}) -> Option AQuick Check:
Useaddfor auto ID = B [OK]
- Using set() without specifying a document ID
- Calling add() on a document reference instead of collection
- Using doc() without an ID then calling add()
db.collection('products').doc('p1').set({name: 'Book', price: 10});
db.collection('products').doc('p1').set({price: 12});Solution
Step 1: Analyze first set call
The first set creates or overwrites document 'p1' with {name: 'Book', price: 10}.Step 2: Analyze second set call without merge
The second set overwrites the entire document with {price: 12}, removing 'name' because merge option is not used.Final Answer:
The document 'p1' will have only {price: 12}, name is removed -> Option CQuick Check:
set without merge overwrites whole document = C [OK]
- Assuming set merges fields by default
- Expecting both name and price to remain
- Thinking calling set twice causes error
db.collection('orders').add({id: 'order1', total: 50});
db.collection('orders').doc('order1').set({total: 60});Solution
Step 1: Understand add() behavior
The first line adds a document with a random ID, ignoring the 'id' field inside data.Step 2: Understand doc('order1').set() behavior
The second line sets data on a document with ID 'order1', which is different from the random ID created before.Step 3: Identify mismatch
Because IDs differ, the second line updates a different document than the one created by add().Final Answer:
The first line creates a document with a random ID, so the second line updates a different document. -> Option BQuick Check:
add() random ID ≠ doc('order1') ID = A [OK]
- Assuming add() uses the id field as document ID
- Using set() on a doc without knowing if it exists
- Expecting add() and set() to affect same document automatically
Solution
Step 1: Understand add() behavior
add()creates a new document with a random ID; it cannot update existing documents.Step 2: Understand set() without merge
set()without options overwrites the whole document, removing other fields.Step 3: Understand set() with merge option
set()with{merge: true}updates only specified fields, keeping others intact.Step 4: Consider update()
update()also updates fields but requires the document to exist;set()with merge works even if document is new.Final Answer:
Useset()with{merge: true}option. -> Option DQuick Check:
Partial update without overwrite = set() + merge true = D [OK]
- Using add() to update existing documents
- Using set() without merge and losing data
- Confusing update() with set() merge behavior
Ready to prove it?
Pass the test and this pattern moves unknown
