But it throws an error about missing index. What should you do?
medium
A. Remove orderBy to fix the error.
B. Create a composite index on 'status' and 'date' fields in Firebase console.
C. Change '==' to '>=' in where clause.
D. Use limit(1) to avoid indexing.
Solution
Step 1: Understand composite index requirement
Queries with where and orderBy on different fields need a composite index in Firebase.
Step 2: Fix error by creating index
Creating a composite index on 'status' and 'date' fields resolves the error.
Final Answer:
Create a composite index on 'status' and 'date' fields in Firebase console. -> Option B
Quick Check:
Missing index error = create composite index [OK]
Hint: Missing index error means create composite index [OK]
Common Mistakes:
Removing orderBy instead of indexing
Changing operators incorrectly
Using limit to bypass index requirement
5. You want to optimize a query to fetch only active users with scores above 50, ordered by score descending, limited to 5 results. Which query is best?
hard
A. db.collection('users').where('active', '==', true).orderBy('score').limit(5)
B. db.collection('users').orderBy('score', 'desc').limit(5).where('active', '==', true).where('score', '>', 50)
C. db.collection('users').where('score', '>', 50).limit(5).orderBy('score', 'desc').where('active', '==', true)
D. db.collection('users').where('active', '==', true).where('score', '>', 50).orderBy('score', 'desc').limit(5)
Solution
Step 1: Check correct chaining and order
db.collection('users').where('active', '==', true).where('score', '>', 50).orderBy('score', 'desc').limit(5) correctly chains where filters first, then orderBy with descending, then limit.
Step 2: Validate query logic
It filters active users with score > 50, orders by score descending, and limits to 5 results as required.
Final Answer:
db.collection('users').where('active', '==', true).where('score', '>', 50).orderBy('score', 'desc').limit(5) -> Option D
Quick Check:
Filter then orderBy desc then limit = optimized query [OK]
Hint: Chain where filters before orderBy and limit [OK]
Common Mistakes:
Placing limit before where
Incorrect orderBy direction
Missing one of the filters
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.