0
0
Firebasecloud~30 mins

Index management in Firebase - Mini Project: Build & Apply

Choose your learning style9 modes available
Firebase Firestore Index Management
📖 Scenario: You are building a simple app to store and retrieve user profiles in Firebase Firestore. To make queries faster and more efficient, you need to create and manage indexes.
🎯 Goal: Learn how to create a Firestore collection, add documents, configure a composite index, and query the collection using the index.
📋 What You'll Learn
Create a Firestore collection called users with specific user documents
Add a configuration variable for the index fields
Write a Firestore query that uses the composite index
Complete the Firestore index configuration to enable the query
💡 Why This Matters
🌍 Real World
Managing indexes in Firestore helps apps run faster and handle complex queries efficiently, especially as data grows.
💼 Career
Understanding Firestore index management is essential for backend developers and cloud engineers working with Firebase to optimize database performance.
Progress0 / 4 steps
1
Create the users collection with documents
Create a Firestore collection called users and add three documents with these exact fields and values: { name: 'Alice', age: 30, city: 'New York' }, { name: 'Bob', age: 25, city: 'Los Angeles' }, and { name: 'Charlie', age: 35, city: 'Chicago' }.
Firebase
Need a hint?

Use firestore.collection('users') to get the collection reference, then add documents with doc().set().

2
Add index configuration for age and city
Create a configuration object called indexConfig with fields collectionGroup set to 'users' and fields set to an array containing { fieldPath: 'age', order: 'ASCENDING' } and { fieldPath: 'city', order: 'ASCENDING' }.
Firebase
Need a hint?

Define indexConfig as an object with collectionGroup and fields array specifying the index fields and order.

3
Write a Firestore query using the composite index
Write a query called queryUsers on the users collection that filters where age is greater than 25 and orders the results by city ascending.
Firebase
Need a hint?

Use where('age', '>', 25) and orderBy('city', 'asc') chained on the users collection.

4
Complete the Firestore index configuration
Add a final property queryScope with value 'COLLECTION' to the indexConfig object to complete the composite index configuration.
Firebase
Need a hint?

Add queryScope: 'COLLECTION' inside the indexConfig object.