0
0
Firebasecloud~20 mins

Subcollections in Firebase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subcollections Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Firestore Subcollections Structure

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?

Aorders/users/user123
Busers/user123/orders
Cusers/orders/user123
Dorders/user123/users
Attempts:
2 left
💡 Hint

Think about the hierarchy: collection > document > subcollection.

Architecture
intermediate
2:00remaining
Best Practice for Storing User Messages in Firestore

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?

ACreate a <code>messages</code> collection with documents named by user ID, each containing all messages as fields.
BCreate a single <code>messages</code> collection with all messages, each document containing a user ID field.
CCreate a <code>users</code> collection, each user document has a <code>messages</code> subcollection with individual message documents.
DStore all messages in a single document inside <code>users</code> collection under a <code>messages</code> field as an array.
Attempts:
2 left
💡 Hint

Consider how Firestore handles large arrays and document size limits.

Configuration
advanced
2:00remaining
Firestore Security Rules for Subcollections

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?

Firebase
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId}/orders/{orderId} {
      allow read, write: if request.auth.uid == userId;
    }
  }
}
Arequest.auth.uid == userId
Brequest.auth.token.email == userId
Crequest.auth.uid != userId
Drequest.auth.token.admin == true
Attempts:
2 left
💡 Hint

Check how to compare authenticated user ID with document path variable.

service_behavior
advanced
2:00remaining
Query Behavior on Firestore Subcollections

You run this query in Firestore:

db.collectionGroup('orders').where('status', '==', 'shipped').get()

What does this query return?

AOnly documents in the <code>orders</code> subcollection of the root collection.
BAn error because <code>collectionGroup</code> cannot be used with subcollections.
CDocuments in the <code>orders</code> subcollection of the first user document only.
DAll documents in all <code>orders</code> subcollections across the database with status 'shipped'.
Attempts:
2 left
💡 Hint

Think about what collectionGroup queries do.

security
expert
3:00remaining
Preventing Unauthorized Access to Nested Subcollections

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?

Firebase
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;
    }
  }
}
Arequest.auth.uid == userId
Brequest.auth.uid == projectId
Crequest.auth.token.email == userId
Drequest.auth.uid == taskId
Attempts:
2 left
💡 Hint

Remember the user ID is in the document path, not project or task IDs.