In Firestore, you have a collection named users. Each user document has a subcollection called orders. What is the correct path to access the orders subcollection for a user with ID user123?
Think about the hierarchy: collection > document > subcollection.
In Firestore, subcollections are nested under a specific document. So to access the orders subcollection for user user123, you first specify the users collection, then the document user123, then the orders subcollection.
You want to store messages sent by users in a Firestore database. Each user can have many messages. Which design uses subcollections correctly and scales well?
Consider how Firestore handles large arrays and document size limits.
Using a messages subcollection under each user document allows efficient querying and avoids document size limits. Large arrays or storing all messages in one document can cause performance and size issues.
You want to write Firestore security rules to allow users to read and write only their own orders subcollection documents. Which rule snippet correctly enforces this?
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /users/{userId}/orders/{orderId} { allow read, write: if request.auth.uid == userId; } } }
Check how to compare authenticated user ID with document path variable.
The rule must check that the authenticated user's ID matches the userId in the document path to restrict access to their own orders.
You run this query in Firestore:
db.collectionGroup('orders').where('status', '==', 'shipped').get()What does this query return?
Think about what collectionGroup queries do.
collectionGroup queries search all subcollections with the given name across the entire database, regardless of parent document.
You have a Firestore database with users collection. Each user document has a projects subcollection, and each project document has a tasks subcollection. You want to ensure users can only read and write tasks in their own projects. Which security rule snippet correctly enforces this?
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /users/{userId}/projects/{projectId}/tasks/{taskId} { allow read, write: if request.auth.uid == userId; } } }
Remember the user ID is in the document path, not project or task IDs.
The rule must check that the authenticated user ID matches the userId in the path to restrict access to their own nested subcollections.