Bird
0
0

You want to model a blog app in Firestore. Each blog post has multiple comments. Which is the best Firestore document model to organize this data?

hard📝 Architecture Q15 of 15
GCP - Cloud Firestore and Bigtable
You want to model a blog app in Firestore. Each blog post has multiple comments. Which is the best Firestore document model to organize this data?
ACreate a 'posts' collection with documents for each post, and a subcollection 'comments' inside each post document
BStore all comments as an array field inside each blog post document
CCreate a single collection 'comments' with a field referencing the post ID
DStore all posts and comments in one flat collection
Step-by-Step Solution
Solution:
  1. Step 1: Consider Firestore best practices for related data

    Large or growing lists like comments should be in subcollections, not arrays, for scalability.
  2. Step 2: Evaluate options for organizing posts and comments

    Using a 'posts' collection with 'comments' subcollections keeps data organized and efficient.
  3. Final Answer:

    Create a 'posts' collection with documents for each post, and a subcollection 'comments' inside each post document -> Option A
  4. Quick Check:

    Use subcollections for related many items [OK]
Quick Trick: Use subcollections for many related items, not arrays [OK]
Common Mistakes:
  • Putting all comments in one array causing size limits
  • Using a flat collection losing post-comment grouping
  • Referencing post ID without subcollections complicates queries

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More GCP Quizzes