📖 Scenario: You are building a simple app to store information about books and their reviews. Each book can have multiple reviews stored inside it as a subcollection.
🎯 Goal: Create a Firestore database structure with a books collection. Inside each book document, create a reviews subcollection to hold review documents.
📋 What You'll Learn
Create a books collection with one book document having ID book1 and a field title with value 'Learn Firebase'.
Create a variable reviewCount set to 0 to track the number of reviews.
Add a review document inside the reviews subcollection of book1 with ID review1 and fields user: 'Alice' and comment: 'Great book!'.
Update the reviewCount field in the book1 document to 1.
💡 Why This Matters
🌍 Real World
Storing related data in subcollections is common in apps like book reviews, social media comments, or orders with items.
💼 Career
Understanding Firestore subcollections is essential for building scalable and organized NoSQL databases in many cloud applications.
Progress0 / 4 steps
1
Create the books collection with one book document
Create a Firestore collection called books. Add a document with ID book1 and a field title set to 'Learn Firebase'.
Firebase
Hint
Use setDoc with doc(db, 'books', 'book1') to create the document.
2
Create a review count variable
Create a variable called reviewCount and set it to 0 to track the number of reviews.
Firebase
Hint
Use let reviewCount = 0; to create the variable.
3
Add a review document inside the reviews subcollection
Add a document with ID review1 inside the reviews subcollection of the book1 document. The review document should have fields user set to 'Alice' and comment set to 'Great book!'.
Firebase
Hint
Use setDoc with doc(db, 'books', 'book1', 'reviews', 'review1') to add the review document.
4
Update the reviewCount field in the book1 document
Update the reviewCount field in the book1 document to 1.
Firebase
Hint
Use setDoc with { merge: true } to update only the reviewCount field.
Practice
(1/5)
1. What is a subcollection in Firebase Firestore?
easy
A. A backup copy of a collection stored in another region
B. A special type of document that stores user credentials
C. A collection nested inside a document to organize related data
D. A collection that automatically deletes old documents
Solution
Step 1: Understand Firestore data structure
Firestore stores data in collections and documents. Documents can contain subcollections.
Step 2: Define subcollection purpose
A subcollection is a collection inside a document used to organize related data hierarchically.
Final Answer:
A collection nested inside a document to organize related data -> Option C
A. Using collection('post1') instead of doc('post1') to access the post document
B. Missing await keyword before set()
C. doc() requires an ID argument
D. Cannot add documents to subcollections
Solution
Step 1: Check the chaining of collection and doc
Subcollections must be accessed by first selecting the parent document with doc('post1'), not collection('post1').
Step 2: Identify the incorrect method call
Using collection('post1') treats 'post1' as a collection, causing an error.
Final Answer:
Using collection('post1') instead of doc('post1') to access the post document -> Option A
Quick Check:
Access document with doc(), not collection() [OK]
Hint: Use doc() for documents, collection() for collections [OK]
Common Mistakes:
Confusing collection() and doc() methods
Assuming doc() always needs an ID
Thinking subcollections can't have new documents
5. You want to model a blog where each post has multiple comments stored as a subcollection. Which approach is best to keep your Firestore database scalable and organized?
hard
A. Store all comments in a single top-level collection named 'comments' with a postId field
B. Store comments as a subcollection inside each post document
C. Store comments inside the post document as an array field
D. Create a separate Firestore database for comments
Solution
Step 1: Consider Firestore best practices for related data
Storing comments as a subcollection inside each post keeps related data grouped and queries efficient.
Step 2: Evaluate other options
Store all comments in a single top-level collection named 'comments' with a postId field can cause large collections and complex queries; Store comments inside the post document as an array field risks document size limits; Create a separate Firestore database for comments is unnecessary complexity.
Final Answer:
Store comments as a subcollection inside each post document -> Option B
Quick Check:
Subcollections keep related data organized and scalable [OK]
Hint: Use subcollections for related lists to keep data organized [OK]
Common Mistakes:
Putting large arrays inside documents risking size limits
Using one big collection causing slow queries
Creating multiple databases unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.