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.