Adding documents (add vs set) in Firebase - Performance Comparison
When adding documents in Firebase, it's important to know how the time to complete the operation changes as you add more documents.
We want to understand how the number of documents affects the work Firebase does when using add() versus set().
Analyze the time complexity of adding multiple documents using add() and set().
const collectionRef = firestore.collection('users');
// Using add() to add a new document with auto ID
collectionRef.add({ name: 'Alice', age: 30 });
// Using set() to add or overwrite a document with a known ID
collectionRef.doc('user123').set({ name: 'Bob', age: 25 });
// Adding multiple documents in a loop
for (let i = 0; i < n; i++) {
collectionRef.add({ index: i });
}
This code adds documents to a collection, either letting Firebase create an ID or specifying one, repeated n times.
Look at what happens repeatedly when adding documents.
- Primary operation: Each call to
add()orset()sends one write request to Firebase. - How many times: The write request happens once per document added, so n times for n documents.
As you add more documents, the number of write requests grows directly with the number of documents.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 write requests |
| 100 | 100 write requests |
| 1000 | 1000 write requests |
Pattern observation: The work grows in a straight line with the number of documents added.
Time Complexity: O(n)
This means the time to add documents grows directly in proportion to how many documents you add.
[X] Wrong: "Using add() is faster than set() because it auto-generates IDs."
[OK] Correct: Both add() and set() send one write request per document, so their time grows the same way with the number of documents.
Understanding how adding documents scales helps you design apps that handle data efficiently and predict costs as your app grows.
"What if we batch multiple document writes together? How would that change the time complexity?"
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
