📖 Scenario: You are building a simple app that stores user profiles in Firebase Firestore. Each profile has a name, age, and city. You want to efficiently get all users from a specific city who are older than a certain age.
🎯 Goal: Build a Firestore query that fetches users from a given city with age greater than a threshold, using query optimization best practices.
📋 What You'll Learn
Create a Firestore collection reference called usersRef pointing to the 'users' collection
Create a variable minAge set to 25
Write a Firestore query called optimizedQuery that fetches users where city equals 'New York' and age is greater than minAge
Add an index hint comment explaining the need for a composite index on city and age
💡 Why This Matters
🌍 Real World
Filtering user data efficiently in a cloud database is common in apps like social networks or marketplaces.
💼 Career
Knowing how to optimize queries and use indexes in Firestore is essential for backend and cloud developers working with Firebase.
Progress0 / 4 steps
1
Create Firestore collection reference
Create a Firestore collection reference called usersRef that points to the 'users' collection using firebase.firestore().collection('users').
Firebase
Hint
Use firebase.firestore().collection('users') to get the collection reference.
2
Set minimum age variable
Create a variable called minAge and set it to 25.
Firebase
Hint
Use const minAge = 25; to create the variable.
3
Write optimized Firestore query
Create a Firestore query called optimizedQuery that fetches users where city equals 'New York' and age is greater than minAge. Use where clauses chained on usersRef.
Firebase
Hint
Chain where clauses on usersRef for city and age.
4
Add index hint comment
Add a comment above optimizedQuery explaining that a composite index on city and age is needed for this query to run efficiently.
Firebase
Hint
Write a comment starting with // Composite index above the query.
Practice
(1/5)
1. What is the main benefit of using query optimization in Firebase?
easy
A. It makes your app faster and reduces costs by fetching only needed data.
B. It automatically backs up your database.
C. It increases the size of your database.
D. It disables security rules temporarily.
Solution
Step 1: Understand query optimization purpose
Query optimization aims to improve performance and reduce costs by limiting data fetched.
Step 2: Identify correct benefit
Only "It makes your app faster and reduces costs by fetching only needed data." describes faster app and cost reduction by fetching needed data.
Final Answer:
It makes your app faster and reduces costs by fetching only needed data. -> Option A
Quick Check:
Query optimization = faster app and lower cost [OK]
Hint: Optimization means fetching only what you need [OK]
Common Mistakes:
Thinking optimization backs up data
Assuming optimization increases database size
Confusing optimization with security settings
2. Which Firebase query syntax correctly limits results to 10 documents ordered by 'score'?
easy
A. db.collection('games').orderBy('score').limit(10)
B. db.collection('games').limit(10).orderBy('score')
C. db.collection('games').where('score', '==', 10).orderBy('score')
D. db.collection('games').orderBy('score', 10)
Solution
Step 1: Check correct method order
In Firebase, orderBy must come before limit to sort then limit results.
Step 2: Validate syntax correctness
db.collection('games').orderBy('score').limit(10) uses correct chaining: orderBy('score').limit(10). Others misuse order or parameters.
Final Answer:
db.collection('games').orderBy('score').limit(10) -> Option A
Quick Check:
orderBy then limit = correct syntax [OK]
Hint: Always orderBy before limit in Firebase queries [OK]
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.