Bird
Raised Fist0
Firebasecloud~5 mins

Index management in Firebase - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you store data in Firebase Firestore, queries can be slow if indexes are missing. Index management helps create and maintain indexes so your app finds data quickly and works smoothly.
When your app needs to search or sort data by multiple fields in Firestore.
When Firestore tells you a query requires a composite index to run.
When you want to improve the speed of complex queries in your Firebase app.
When you add new query patterns that Firestore does not support by default.
When you want to keep your Firestore indexes organized and up to date.
Config File - firestore.indexes.json
firestore.indexes.json
{
  "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.

Commands
Lists all current indexes in your Firestore database so you can see what is already set up.
Terminal
firebase firestore:indexes
Expected OutputExpected
Name: projects/my-project/databases/(default)/collectionGroups/orders/indexes/abc123 State: READY Name: projects/my-project/databases/(default)/collectionGroups/users/indexes/def456 State: READY
Uploads and applies the indexes defined in firestore.indexes.json to your Firestore database.
Terminal
firebase deploy --only firestore:indexes
Expected OutputExpected
=== Deploying to 'my-project'... āœ” firestore:indexes: Successfully deployed indexes. Project Console: https://console.firebase.google.com/project/my-project/firestore/indexes āœ” Deploy complete!
→
--only - Deploys only the specified Firebase feature, here indexes.
Shows a detailed list of all indexes currently active in Firestore after deployment.
Terminal
firebase firestore:indexes:list
Expected OutputExpected
Collection Group: orders Fields: status (ASCENDING) createdAt (DESCENDING) State: READY Collection Group: users Fields: lastName (ASCENDING) firstName (ASCENDING) State: READY
Key Concept

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.

Common Mistakes
Not deploying the index file after creating or updating it.
Firestore will not use the new indexes until they are deployed, causing queries to fail or be slow.
Always run 'firebase deploy --only firestore:indexes' after changing your index definitions.
Trying to run queries that require composite indexes without creating those indexes first.
Firestore rejects queries without the needed indexes, returning an error with a link to create the index.
Use the error link to create the required index or add it manually in firestore.indexes.json and deploy.
Summary
Use firestore.indexes.json to define composite indexes for your Firestore collections.
Deploy indexes with 'firebase deploy --only firestore:indexes' to make them active.
Check existing indexes with 'firebase firestore:indexes' or 'firebase firestore:indexes:list'.

Practice

(1/5)
1.

What is the main purpose of indexes in Firebase Firestore?

easy
A. To speed up searching and sorting data
B. To store large files like images
C. To backup the database automatically
D. To encrypt data for security

Solution

  1. Step 1: Understand what indexes do in databases

    Indexes help the database find data faster by organizing it efficiently.
  2. Step 2: Apply this to Firebase Firestore

    In Firestore, indexes speed up searching and sorting operations on data.
  3. Final Answer:

    To speed up searching and sorting data -> Option A
  4. Quick Check:

    Indexes improve query speed = B [OK]
Hint: Indexes speed up queries by organizing data [OK]
Common Mistakes:
  • Confusing indexes with file storage
  • Thinking indexes backup data
  • Assuming indexes encrypt data
2.

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"}
      ]
    }
  ]
}
easy
A. Defines a composite index on 'age' ascending and 'name' descending for 'users' collection
B. Defines a single-field index only on 'age' field
C. Defines an index for storing files in 'users' collection
D. Defines a backup schedule for 'users' collection

Solution

  1. Step 1: Read the JSON structure for indexes

    The JSON shows two fields with order specified, which means a composite index.
  2. Step 2: Match fields and collectionGroup

    It targets the 'users' collection group with 'age' ascending and 'name' descending order.
  3. Final Answer:

    Defines a composite index on 'age' ascending and 'name' descending for 'users' collection -> Option A
  4. Quick Check:

    Composite index JSON matches Defines a composite index on 'age' ascending and 'name' descending for 'users' collection [OK]
Hint: Composite index JSON lists multiple fields with order [OK]
Common Mistakes:
  • Thinking it defines a single-field index
  • Confusing index with file storage or backup
  • Ignoring the order property in fields
3.

Given this Firestore query:

db.collection('orders').where('status', '==', 'shipped').orderBy('date', 'desc')

Which index is required to support this query?

medium
A. No index needed for this query
B. Single-field index on 'date' only
C. Composite index on fields 'status' (ASCENDING) and 'date' (DESCENDING)
D. Composite index on 'date' (ASCENDING) and 'status' (DESCENDING)

Solution

  1. Step 1: Identify query filters and sort

    The query filters by 'status' equal to 'shipped' and orders by 'date' descending.
  2. 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.
  3. Final Answer:

    Composite index on fields 'status' (ASCENDING) and 'date' (DESCENDING) -> Option C
  4. Quick Check:

    Equality + orderBy needs composite index = A [OK]
Hint: Equality filters + orderBy need composite index [OK]
Common Mistakes:
  • Assuming single-field index suffices
  • Mixing order directions in index
  • Thinking no index is needed
4.

You deployed a new composite index but your Firestore query still fails with an error about missing index. What is the most likely cause?

medium
A. You need to restart your app to apply index changes
B. You forgot to run firebase deploy --only firestore:indexes after editing firestore.indexes.json
C. You used the wrong database URL
D. Your query does not need any index

Solution

  1. Step 1: Understand index deployment process

    Editing firestore.indexes.json only changes local config; deployment is needed to update Firestore.
  2. Step 2: Identify missing deployment command

    If you forget to run firebase deploy --only firestore:indexes, Firestore won't have the new index.
  3. Final Answer:

    You forgot to run firebase deploy --only firestore:indexes after editing firestore.indexes.json -> Option B
  4. Quick Check:

    Index changes require deploy command = D [OK]
Hint: Always deploy indexes after editing JSON file [OK]
Common Mistakes:
  • Assuming query needs no index
  • Thinking app restart applies index
  • Confusing database URL with index issues
5.

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?

hard
A. It stores large files related to 'products'
B. It backs up the 'products' collection automatically
C. It encrypts the 'products' data for security
D. It supports filtering on 'category' and 'price' and sorting by 'rating' efficiently

Solution

  1. Step 1: Analyze query requirements

    The query filters on 'category' and 'price' and sorts by 'rating'. All three fields must be indexed.
  2. Step 2: Understand composite index role

    A composite index with these fields and orders allows Firestore to quickly find and sort matching documents.
  3. 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.
  4. Final Answer:

    It supports filtering on 'category' and 'price' and sorting by 'rating' efficiently -> Option D
  5. Quick Check:

    Composite index matches filters + sort = A [OK]
Hint: Composite index must cover all filters and sort fields [OK]
Common Mistakes:
  • Thinking index backs up or encrypts data
  • Ignoring the order of fields in index
  • Creating single-field indexes only
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard