0
0
Firebasecloud~30 mins

Query optimization in Firebase - Mini Project: Build & Apply

Choose your learning style9 modes available
Query optimization
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Write a comment starting with // Composite index above the query.