0
0
Firebasecloud~30 mins

Subcollections in Firebase - Mini Project: Build & Apply

Choose your learning style9 modes available
Firebase Subcollections Setup
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use setDoc with { merge: true } to update only the reviewCount field.