Creating collections and documents in Firebase - Performance & Efficiency
When we create collections and documents in Firebase, we want to know how the time it takes changes as we add more data.
We ask: How does the number of operations grow when we add many documents?
Analyze the time complexity of the following operation sequence.
const db = getFirestore();
for (let i = 0; i < n; i++) {
const docRef = doc(collection(db, 'users'));
await setDoc(docRef, { name: `User ${i}`, age: 20 + i });
}
This code creates a new document inside the 'users' collection for each user, setting simple data.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Calling
setDocto write each document. - How many times: Exactly once per document, so
ntimes.
Each new document requires one write operation, so as the number of documents grows, the total operations grow the same way.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 writes |
| 100 | 100 writes |
| 1000 | 1000 writes |
Pattern observation: The number of operations grows directly with the number of documents added.
Time Complexity: O(n)
This means the time to create documents grows in a straight line as you add more documents.
[X] Wrong: "Creating multiple documents happens all at once, so time stays the same no matter how many documents."
[OK] Correct: Each document write is a separate operation that takes time, so more documents mean more total time.
Understanding how operations grow helps you design efficient data writes and shows you can think about scaling in real projects.
"What if we batch all document writes into a single batch operation? How would the time complexity change?"
Practice
Solution
Step 1: Understand Firestore structure
Firestore organizes data in collections and documents.Step 2: Define collection role
A collection groups multiple documents together.Final Answer:
A group that holds multiple documents. -> Option CQuick Check:
Collection = group of documents [OK]
- Confusing collection with a single document
- Thinking collection is a method
- Mixing collection with data fields
Solution
Step 1: Recall document creation methods
Firestore uses set() to create or overwrite documents with specific IDs.Step 2: Identify method for auto-generated IDs
The add() method creates a document with an auto-generated ID inside a collection.Final Answer:
add() -> Option BQuick Check:
Auto-ID document creation = add() [OK]
- Using set() and expecting auto-ID
- Confusing update() with creation
- Using get() which only reads data
db.collection('users').doc('user1').set({name: 'Anna', age: 25});Solution
Step 1: Analyze method usage
set() on a doc reference creates or overwrites that document.Step 2: Check document ID and data
The doc ID is 'user1', so this document will be created or replaced with given data.Final Answer:
Creates or overwrites 'user1' document with name and age. -> Option AQuick Check:
set() on doc('user1') = overwrite/create [OK]
- Thinking add() is needed for specific IDs
- Assuming update() is called by set()
- Confusing set() with partial update
db.collection('products').add({name: 'Book', price: 10}).doc('p1').set({price: 12});Solution
Step 1: Understand add() return value
add() returns a promise resolving to a document reference, not a direct reference.Step 2: Check chaining doc() after add()
Calling doc() on the promise or document reference from add() is invalid syntax.Final Answer:
add() returns a promise, chaining doc() is invalid. -> Option DQuick Check:
add() returns promise, cannot chain doc() directly [OK]
- Chaining doc() immediately after add()
- Ignoring promise returned by add()
- Assuming set() cannot update fields
Solution
Step 1: Identify method to create document with specific ID
Use doc('order123') to specify the document ID explicitly.Step 2: Use set() to create or overwrite document data
set() writes the data to the specified document ID.Final Answer:
db.collection('orders').doc('order123').set({item: 'Pen', quantity: 3}); -> Option AQuick Check:
Specific ID document creation = doc(id).set() [OK]
- Using add() with id field (does not set document ID)
- Calling doc() without ID for specific ID
- Using set() directly on collection
Ready to prove it?
Pass the test and this pattern moves unknown
