0
0
Firebasecloud~10 mins

Collection group queries in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Collection group queries
Start Query
Specify Collection Group
Search All Subcollections Named Same
Apply Filters/Conditions
Retrieve Matching Documents
Return Results
The query starts by specifying a collection group name, searches all subcollections with that name across the database, applies any filters, and returns matching documents.
Execution Sample
Firebase
db.collectionGroup('messages')
  .where('read', '==', false)
  .get()
  .then(snapshot => {
    // process documents
  });
This query finds all documents in any 'messages' subcollection where 'read' is false.
Process Table
StepActionTargetFilter AppliedDocuments Found
1Start querycollectionGroup('messages')nonenone
2Search all 'messages' subcollectionsAll 'messages' subcollectionsnoneall documents in these subcollections
3Apply filterread == falsefilter documents where read is falsesubset of documents
4Return resultsfiltered documentsnonedocuments matching filter
💡 Query completes after filtering all 'messages' subcollections and returning matching documents.
Status Tracker
VariableStartAfter Step 2After Step 3Final
documentsemptyall documents in 'messages' subcollectionsfiltered documents where read == falsefiltered documents
Key Moments - 2 Insights
Why does the query search multiple subcollections instead of just one?
Because collection group queries look across all subcollections with the same name anywhere in the database, as shown in execution_table step 2.
How does the filter affect the documents returned?
The filter narrows down documents to only those matching the condition (read == false), as shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step are documents filtered by the 'read' field?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Check the 'Filter Applied' column in the execution_table rows.
According to variable_tracker, what is the state of 'documents' after step 2?
AEmpty
BFiltered documents where read == false
CAll documents in 'messages' subcollections
DFinal filtered documents
💡 Hint
Look at the 'After Step 2' column for 'documents' in variable_tracker.
If the filter condition changed to 'read == true', how would the execution_table row 3 change?
AFilter would be 'read == true' and documents found would be those with read true
BNo change, filter stays the same
CFilter would be removed
DDocuments found would be all documents
💡 Hint
Refer to the 'Filter Applied' and 'Documents Found' columns in execution_table row 3.
Concept Snapshot
Collection group queries search all subcollections with the same name across the database.
Use collectionGroup('name') to specify the group.
Apply filters with where() to narrow results.
Returns documents from all matching subcollections.
Useful for querying nested data without knowing exact paths.
Full Transcript
Collection group queries in Firebase let you search all subcollections with the same name anywhere in your database. You start by specifying the collection group name. Then the query looks through every subcollection with that name. After that, you can apply filters to find only documents that match certain conditions. Finally, the query returns all documents that meet the criteria. This is helpful when your data is nested and you want to find documents without knowing their exact location. For example, you can find all unread messages across all users by querying the 'messages' collection group where 'read' is false.