Index management in Firebase - Commands & Configuration
{
"indexes": [
{
"collectionGroup": "orders",
"queryScope": "COLLECTION",
"fields": [
{ "fieldPath": "status", "order": "ASCENDING" },
{ "fieldPath": "createdAt", "order": "DESCENDING" }
]
},
{
"collectionGroup": "users",
"queryScope": "COLLECTION",
"fields": [
{ "fieldPath": "lastName", "order": "ASCENDING" },
{ "fieldPath": "firstName", "order": "ASCENDING" }
]
}
],
"fieldOverrides": []
}This file defines composite indexes for Firestore queries.
indexes: Lists collections and fields to index together for faster queries.
collectionGroup: The collection name to index.
fields: Fields and their sort order to include in the index.
queryScope: Limits the index to the collection only.
firebase firestore:indexes
firebase deploy --only firestore:indexes
--only - Deploys only the specified Firebase feature, here indexes.firebase firestore:indexes:list
If you remember nothing else from this pattern, remember: Firestore needs indexes for complex queries, and you manage them by defining and deploying index files.
Practice
What is the main purpose of indexes in Firebase Firestore?
Solution
Step 1: Understand what indexes do in databases
Indexes help the database find data faster by organizing it efficiently.Step 2: Apply this to Firebase Firestore
In Firestore, indexes speed up searching and sorting operations on data.Final Answer:
To speed up searching and sorting data -> Option AQuick Check:
Indexes improve query speed = B [OK]
- Confusing indexes with file storage
- Thinking indexes backup data
- Assuming indexes encrypt data
Which of the following is the correct way to define a composite index in firestore.indexes.json?
{
"indexes": [
{
"collectionGroup": "users",
"queryScope": "COLLECTION",
"fields": [
{"fieldPath": "age", "order": "ASCENDING"},
{"fieldPath": "name", "order": "DESCENDING"}
]
}
]
}Solution
Step 1: Read the JSON structure for indexes
The JSON shows two fields with order specified, which means a composite index.Step 2: Match fields and collectionGroup
It targets the 'users' collection group with 'age' ascending and 'name' descending order.Final Answer:
Defines a composite index on 'age' ascending and 'name' descending for 'users' collection -> Option AQuick Check:
Composite index JSON matches Defines a composite index on 'age' ascending and 'name' descending for 'users' collection [OK]
- Thinking it defines a single-field index
- Confusing index with file storage or backup
- Ignoring the order property in fields
Given this Firestore query:
db.collection('orders').where('status', '==', 'shipped').orderBy('date', 'desc')Which index is required to support this query?
Solution
Step 1: Identify query filters and sort
The query filters by 'status' equal to 'shipped' and orders by 'date' descending.Step 2: Determine required index fields and order
Firestore needs a composite index with 'status' ascending (default for equality) and 'date' descending as per orderBy.Final Answer:
Composite index on fields 'status' (ASCENDING) and 'date' (DESCENDING) -> Option CQuick Check:
Equality + orderBy needs composite index = A [OK]
- Assuming single-field index suffices
- Mixing order directions in index
- Thinking no index is needed
You deployed a new composite index but your Firestore query still fails with an error about missing index. What is the most likely cause?
Solution
Step 1: Understand index deployment process
Editingfirestore.indexes.jsononly changes local config; deployment is needed to update Firestore.Step 2: Identify missing deployment command
If you forget to runfirebase deploy --only firestore:indexes, Firestore won't have the new index.Final Answer:
You forgot to runfirebase deploy --only firestore:indexesafter editingfirestore.indexes.json-> Option BQuick Check:
Index changes require deploy command = D [OK]
- Assuming query needs no index
- Thinking app restart applies index
- Confusing database URL with index issues
You want to optimize a Firestore query that filters by category and price, and sorts by rating. Which index should you create?
{
"collectionGroup": "products",
"queryScope": "COLLECTION",
"fields": [
{"fieldPath": "category", "order": "ASCENDING"},
{"fieldPath": "price", "order": "ASCENDING"},
{"fieldPath": "rating", "order": "DESCENDING"}
]
}Why is this index necessary?
Solution
Step 1: Analyze query requirements
The query filters on 'category' and 'price' and sorts by 'rating'. All three fields must be indexed.Step 2: Understand composite index role
A composite index with these fields and orders allows Firestore to quickly find and sort matching documents.Step 3: Confirm index JSON matches query needs
The JSON defines a composite index on 'category' ASC, 'price' ASC, and 'rating' DESC, matching the query.Final Answer:
It supports filtering on 'category' and 'price' and sorting by 'rating' efficiently -> Option DQuick Check:
Composite index matches filters + sort = A [OK]
- Thinking index backs up or encrypts data
- Ignoring the order of fields in index
- Creating single-field indexes only
Ready to prove it?
Pass the test and this pattern moves unknown
