0
0
Firebasecloud~10 mins

Subcollections in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Subcollections
Start: Access Root Collection
Select Document in Root
Access Subcollection inside Document
Perform Operation (Read/Write) on Subcollection Document
End
Access starts at a root collection, then a document inside it, then a subcollection inside that document, where operations happen.
Execution Sample
Firebase
db.collection('users').doc('user1').collection('orders').doc('order1').get()
This code reads the document 'order1' inside the 'orders' subcollection of the 'user1' document in the 'users' collection.
Process Table
StepActionTargetResult
1Access collection'users'Reference to 'users' collection
2Select document'user1'Reference to 'user1' document
3Access subcollection'orders'Reference to 'orders' subcollection inside 'user1'
4Select document'order1'Reference to 'order1' document inside 'orders'
5Get document data'order1'Returns data of 'order1' document
6EndN/AOperation complete
💡 Operation ends after retrieving the 'order1' document data from the subcollection.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
dbRefFirebase DB rootusers collection refuser1 doc reforders subcollection reforder1 doc reforder1 doc dataorder1 doc data
Key Moments - 2 Insights
Why do we need to select a document before accessing a subcollection?
Because subcollections belong inside specific documents, you must first select the document to reach its subcollection, as shown in steps 2 and 3 of the execution_table.
Can we access a subcollection directly without specifying the parent document?
No, subcollections are nested inside documents, so you must specify the parent document first (step 2) before accessing the subcollection (step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the target at step 3?
A'user1' document inside 'users' collection
B'order1' document inside 'orders' subcollection
C'orders' subcollection inside 'user1' document
D'users' collection
💡 Hint
Refer to the 'Target' column in row for step 3 in execution_table.
At which step do we get the actual data of the 'order1' document?
AStep 5
BStep 4
CStep 2
DStep 3
💡 Hint
Check the 'Action' and 'Result' columns in execution_table for when data is retrieved.
If we skip step 2 and try to access 'orders' subcollection directly, what happens?
AWe get a reference to 'orders' subcollection of the root
BError or invalid reference because subcollections need a parent document
CWe access all 'orders' subcollections in the database
DWe get the 'orders' collection at root level
💡 Hint
Recall key_moments about needing to select a document before subcollection.
Concept Snapshot
Subcollections are collections inside documents.
Access flow: collection -> document -> subcollection -> document.
You must specify the parent document before accessing its subcollection.
Operations on subcollections are like normal collections but nested.
Useful for organizing related data hierarchically.
Full Transcript
Subcollections in Firebase are collections nested inside documents. To access data in a subcollection, you first select the root collection, then the specific document inside it, then the subcollection inside that document, and finally the document inside the subcollection. For example, to get an order of a user, you access 'users' collection, then 'user1' document, then 'orders' subcollection, then 'order1' document. This nested structure helps organize data logically. You cannot access a subcollection without specifying its parent document first. The execution steps show how references move from root collection down to the subcollection document, and finally retrieve the data. This step-by-step access ensures data is well organized and queries are precise.