What will db.collection('users').doc('user123').collection('posts').get() return?
medium
A. A list of all posts documents under user123
B. An error because posts is not a field
C. Only the user123 document data
D. All users documents with their posts
Solution
Step 1: Understand nested collections
In Firestore, collections can be nested under documents. Here, posts is a subcollection under user123.
Step 2: What does the query do?
The query gets all documents in the posts collection under user123, so it returns all posts documents.
Final Answer:
A list of all posts documents under user123 -> Option A
Quick Check:
Subcollections return their documents on get() [OK]
Hint: Subcollections are accessed by chaining collection().doc().collection() [OK]
Common Mistakes:
Thinking subcollections are fields inside documents
Expecting get() to return parent document data
Assuming get() returns all users and posts together
4. You have this Firestore security rule snippet:
match /users/{userId} {
allow read, write: if request.auth.uid == userId;
}
What is the main issue if you want users to read their posts stored in a subcollection posts under their user document?
medium
A. The rule does not cover the posts subcollection, so access is denied
B. The rule allows anyone to read posts
C. The rule allows write but not read
D. The rule only works for admin users
Solution
Step 1: Analyze the rule scope
The rule matches only the /users/{userId} document, not any subcollections like posts.
Step 2: Effect on subcollections
Without explicit rules for /users/{userId}/posts/{postId}, access to posts is denied by default.
Final Answer:
The rule does not cover the posts subcollection, so access is denied -> Option A
Quick Check:
Security rules must cover subcollections explicitly [OK]
Hint: Rules must match subcollections separately to allow access [OK]
Common Mistakes:
Assuming parent document rules apply to subcollections
Believing rules allow all reads by default
Confusing read and write permissions
5. You want to design a Firestore data model for a chat app where users can have many messages. Which approach follows best practices for scalability and easy querying?
hard
A. Store all messages in a single collection with a userId field to filter messages
B. Store messages as an array field inside each user document
C. Create a messages subcollection under each user document
D. Store messages in a single document with nested objects for each message
Solution
Step 1: Consider scalability needs
Storing many messages inside one document or array can cause size limits and slow queries.
Step 2: Use subcollections for many related items
Creating a messages subcollection under each user document allows efficient queries and scales well.
Final Answer:
Create a messages subcollection under each user document -> Option C
Quick Check:
Use subcollections for large, related data sets [OK]
Hint: Use subcollections for many related items to avoid document size limits [OK]
Common Mistakes:
Putting many items in arrays inside documents
Using one big collection without filtering
Storing all data in one document causing size errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.