0
0
Firebasecloud~20 mins

Collection group queries in Firebase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Collection Group Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What documents does this collection group query return?

Consider a Firestore database with multiple subcollections named comments under different posts. Which documents will this query return?

const query = firestore.collectionGroup('comments').where('approved', '==', true);
Firebase
const query = firestore.collectionGroup('comments').where('approved', '==', true);
AAll documents in the root collection where 'approved' is true.
BOnly documents in the 'comments' subcollection of the root collection where 'approved' is true.
CAll documents in any 'comments' subcollection where the 'approved' field is true.
DDocuments in any collection named 'comments' but only if they are direct children of the root.
Attempts:
2 left
💡 Hint

Think about what collectionGroup does: it searches all subcollections with the given name, regardless of their parent.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this collection group query

Which option contains the correct syntax for querying all documents in subcollections named reviews where the rating is greater than 4?

Firebase
firestore.collectionGroup('reviews').where('rating', '>', 4).get()
Afirestore.collectionGroup('reviews').where('rating' > 4).get()
Bfirestore.collectionGroup('reviews').where('rating', '>', 4).get()
Cfirestore.collectionGroup('reviews').where('rating', '=>', 4).get()
Dfirestore.collectionGroup('reviews').where('rating', 'gt', 4).get()
Attempts:
2 left
💡 Hint

Check the syntax of the where method parameters carefully.

optimization
advanced
2:30remaining
How to optimize a collection group query with multiple filters?

You want to query all orders subcollections for documents where status is 'shipped' and total is greater than 100. Which approach optimizes performance best?

AUse a collection group query with a single <code>where</code> filter on <code>status</code> and ignore the <code>total</code> filter.
BRun two separate collection group queries, one filtering by <code>status</code> and one by <code>total</code>, then merge results in code.
CQuery the root collection and filter results in the client code after fetching all documents.
DUse a collection group query with two <code>where</code> filters: <code>.where('status', '==', 'shipped').where('total', '>', 100)</code> and create a composite index for these fields.
Attempts:
2 left
💡 Hint

Think about how Firestore uses indexes to speed up queries with multiple filters.

🔧 Debug
advanced
2:30remaining
Why does this collection group query fail with a missing index error?

This query fails with an error about a missing index:

firestore.collectionGroup('messages').where('sender', '==', 'alice').where('read', '==', false).get()

What is the cause?

AFirestore requires a composite index on 'sender' and 'read' fields for collection group queries with multiple filters.
BThe collection group name 'messages' is invalid and causes the error.
CThe query uses '==' operator twice which is not allowed in collection group queries.
DFirestore does not support filtering on boolean fields like 'read' in collection group queries.
Attempts:
2 left
💡 Hint

Check Firestore's indexing requirements for collection group queries with multiple filters.

🧠 Conceptual
expert
3:00remaining
What is a key limitation of collection group queries in Firestore?

Which of the following is a true limitation when using collection group queries?

ACollection group queries cannot include <code>orderBy</code> clauses on fields not included in the <code>where</code> filters without an index.
BYou cannot perform collection group queries on collections with different field structures.
CCollection group queries only work on root-level collections, not subcollections.
DYou can only query one collection group per Firestore database.
Attempts:
2 left
💡 Hint

Think about Firestore's indexing rules and how they affect ordering in queries.