📖 Scenario: You are building a simple app to store user profiles in a cloud database. Each user has a name and an email address. You will create a collection called users and add documents for each user.
🎯 Goal: Create a Firestore collection named users and add user documents with name and email fields.
📋 What You'll Learn
Create a Firestore collection called users
Add a document for a user with name and email fields
Add a second user document with the same fields
Use Firestore's setDoc method to add documents
💡 Why This Matters
🌍 Real World
Storing user profiles or any structured data in a cloud database for web or mobile apps.
💼 Career
Understanding how to create collections and documents in Firestore is essential for backend and full-stack developers working with Firebase.
Progress0 / 4 steps
1
Initialize Firestore and create the users collection reference
Write code to initialize Firestore and create a collection reference called usersRef for the users collection.
Firebase
Hint
Use collection(db, 'users') to create the collection reference.
2
Create a user object to add
Create a constant object called user1 with name set to 'Alice' and email set to 'alice@example.com'.
Firebase
Hint
Use const user1 = { name: 'Alice', email: 'alice@example.com' }.
3
Add the first user document to the users collection
Write code to add user1 as a document to the usersRef collection using the setDoc method with a document reference named user1Doc created by doc(usersRef, 'user1').
Firebase
Hint
Use doc(usersRef, 'user1') to create the document reference and setDoc to add the data.
4
Add a second user document to the users collection
Create a constant object called user2 with name set to 'Bob' and email set to 'bob@example.com'. Then create a document reference called user2Doc using doc(usersRef, 'user2') and add user2 to the collection using setDoc.
Firebase
Hint
Repeat the steps for user1 but with user2 and user2Doc.
Practice
(1/5)
1. What is a collection in Firebase Firestore?
easy
A. A function to add data.
B. A single document storing data.
C. A group that holds multiple documents.
D. A method to delete documents.
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 C
Quick Check:
Collection = group of documents [OK]
Hint: Collections hold documents, not single data items. [OK]
Common Mistakes:
Confusing collection with a single document
Thinking collection is a method
Mixing collection with data fields
2. Which Firebase Firestore method creates a document with an auto-generated ID?
easy
A. set()
B. add()
C. update()
D. get()
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 B
Quick Check:
Auto-ID document creation = add() [OK]
Hint: Use add() for auto-generated document IDs. [OK]
Hint: add() returns promise; await before doc() calls. [OK]
Common Mistakes:
Chaining doc() immediately after add()
Ignoring promise returned by add()
Assuming set() cannot update fields
5. You want to create a collection 'orders' and add a document with a specific ID 'order123' containing {item: 'Pen', quantity: 3}. Which code snippet is correct?
hard
A. db.collection('orders').doc('order123').set({item: 'Pen', quantity: 3});
B. db.collection('orders').add({item: 'Pen', quantity: 3, id: 'order123'});
C. db.collection('orders').doc().set({item: 'Pen', quantity: 3, id: 'order123'});
D. db.collection('orders').set('order123', {item: 'Pen', quantity: 3});
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 A
Quick Check:
Specific ID document creation = doc(id).set() [OK]
Hint: Use doc('id').set() for specific document IDs. [OK]
Common Mistakes:
Using add() with id field (does not set document ID)
Calling doc() without ID for specific ID
Using set() directly on collection
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.