Firestore Single Field Index Exemption
Introduction
Section titled “Introduction”By default, Firestore automatically creates single-field indexes for every field in every document. For each non-array and non-map field, Firestore creates:
- One ascending index
- One descending index
While this makes querying flexible, there are two main reasons why you should use index exemptions:
1. Reduce Cost of Unwanted Indexes
Section titled “1. Reduce Cost of Unwanted Indexes”This is the primary reason to use index exemptions. Every index consumes storage and adds write latency:
- Storage costs - Each index entry takes up space. For large collections with millions of documents, indexing unused fields wastes significant storage.
- Write latency - Index fanout (updating multiple indexes on every write) is the main contributor to write latency. Fewer indexes = faster writes.
- Billing impact - You pay for index storage. Exempting fields you never query can reduce your Firestore bill substantially.
Example: A products collection with 1 million documents, each having a description field (never queried). That’s 2 million unnecessary index entries (ascending + descending) just for one field.
2. The “Too Many Index Entries” Error
Section titled “2. The “Too Many Index Entries” Error”One common issue you may encounter is the error:
Too many index entries for entityFirestore has a limit of 40,000 index entries per document. When a document exceeds this limit, writes will fail with this error.
What Causes It
Section titled “What Causes It”Since Firestore automatically creates single-field indexes for all fields (including nested maps and array items), index entries can accumulate quickly:
- Deeply nested field structures - Each nested field in a map gets indexed
- Large arrays - Each array element creates separate index entries
- Accumulated data over time - Documents that grow (like analytics, logs, or history) can eventually hit the limit
- Composite indexes - These multiply index entries further
Example Scenario
Section titled “Example Scenario”A document storing user activity with nested data:
{ userId: "user123", activities: [ { type: "login", timestamp: ..., metadata: { device: ..., location: ... } }, { type: "purchase", timestamp: ..., metadata: { items: [...], total: ... } }, // ... hundreds more activities ]}Each activity and its nested fields generate multiple index entries, quickly approaching the 40,000 limit.
Solution
Section titled “Solution”Add an exemption to exclude problematic fields from indexing:
{ "fieldOverrides": [ { "collectionGroup": "users", "fieldPath": "activities", "indexes": [] } ]}Or via Firebase Console:
- Go to Firestore > Indexes > Single field
- Click “Add exemption”
- Enter the collection ID and field path to exclude
- Select query scope and confirm
For more details, see: Too Many Index Entries for Entity
When to Exempt Fields
Section titled “When to Exempt Fields”You should exempt fields from automatic indexing when:
- Large string fields - Fields containing long text, URLs, or descriptions
- Large array or map fields - Complex nested data structures
- High write-rate documents - Documents that are updated frequently
- Fields never used in queries - Metadata, logs, or tracking fields
- Fields only used for display - Not filtered or sorted
Common Fields to Exempt
Section titled “Common Fields to Exempt”- description, content, body (long text)- metadata, settings, config (large maps)- logs, history, audit trails- base64 encoded data- URLs, file paths- Timestamps only used for display (not querying)How to Configure Index Exemptions
Section titled “How to Configure Index Exemptions”Using firestore.indexes.json
Section titled “Using firestore.indexes.json”Add exemptions in the fieldOverrides section:
{ "indexes": [ // Your composite indexes ], "fieldOverrides": [ { "collectionGroup": "products", "fieldPath": "description", "indexes": [] }, { "collectionGroup": "orders", "fieldPath": "metadata", "indexes": [] }, { "collectionGroup": "logs", "fieldPath": "*", "indexes": [] } ]}Exemption Options
Section titled “Exemption Options”Disable all indexing for a field:
{ "collectionGroup": "products", "fieldPath": "description", "indexes": []}Disable only descending and array indexing (recommended default):
{ "collectionGroup": "products", "fieldPath": "someField", "indexes": [ { "order": "ASCENDING", "queryScope": "COLLECTION" } ]}Collection-level exemption for all fields:
{ "collectionGroup": "logs", "fieldPath": "*", "indexes": []}Using Firebase Console
Section titled “Using Firebase Console”- Go to Firebase Console > Firestore Database > Indexes
- Click on “Single Field” tab
- Click “Add exemption”
- Select collection and field
- Choose indexing settings
Best Practices
Section titled “Best Practices”1. Audit Your Collections
Section titled “1. Audit Your Collections”Before exempting, identify:
- Which fields are actually used in queries
- Which fields have large values
- Which collections have high write rates
2. Recommended Default Strategy
Section titled “2. Recommended Default Strategy”For most applications, disable Descending & Array indexing by default, keeping only Ascending:
{ "collectionGroup": "yourCollection", "fieldPath": "fieldName", "indexes": [ { "order": "ASCENDING", "queryScope": "COLLECTION" } ]}3. Exempt Log Collections Entirely
Section titled “3. Exempt Log Collections Entirely”For logging collections that are rarely queried:
{ "collectionGroup": "webhookLogs", "fieldPath": "*", "indexes": []},{ "collectionGroup": "apiLogs", "fieldPath": "*", "indexes": []},{ "collectionGroup": "errorLogs", "fieldPath": "*", "indexes": []}4. Keep Essential Indexes
Section titled “4. Keep Essential Indexes”Always keep indexes for fields used in:
where()clausesorderBy()clauses- Composite index requirements
When Index is Required
Section titled “When Index is Required”| Query Pattern | Index Needed? |
|---|---|
Single field where() | Auto-indexed (can exempt if not used) |
where() + orderBy() different fields | YES (composite) |
Multiple where() with inequality | YES (composite) |
where() same field as orderBy() | Auto-indexed |
Example Configuration
Section titled “Example Configuration”Here’s a comprehensive example for a typical Shopify app:
{ "indexes": [ { "collectionGroup": "orders", "queryScope": "COLLECTION", "fields": [ { "fieldPath": "shopId", "order": "ASCENDING" }, { "fieldPath": "createdAt", "order": "DESCENDING" } ] } ], "fieldOverrides": [ { "collectionGroup": "shops", "fieldPath": "settings", "indexes": [] }, { "collectionGroup": "shops", "fieldPath": "accessToken", "indexes": [] }, { "collectionGroup": "products", "fieldPath": "description", "indexes": [] }, { "collectionGroup": "products", "fieldPath": "images", "indexes": [] }, { "collectionGroup": "webhookLogs", "fieldPath": "*", "indexes": [] }, { "collectionGroup": "syncLogs", "fieldPath": "*", "indexes": [] } ]}Deploying Index Changes
Section titled “Deploying Index Changes”After updating firestore.indexes.json, deploy with:
firebase deploy --only firestore:indexesWill exempting indexes break existing queries?
Section titled “Will exempting indexes break existing queries?”Yes, if you exempt a field that is used in a query’s where() or orderBy() clause, that query will fail. Always check your codebase before exempting.
Can I re-enable indexing after exempting?
Section titled “Can I re-enable indexing after exempting?”Yes, simply remove the exemption from fieldOverrides or update the indexes array. The field will revert to automatic indexing.
How much storage can I save?
Section titled “How much storage can I save?”This varies by your data. For collections with large text fields or high document counts, savings can be significant (10-30% of index storage costs).
Does this affect read performance?
Section titled “Does this affect read performance?”No, exemptions only affect which queries are possible. Reads of exempted fields work normally.
See Also
Section titled “See Also”Firestore Guides:
- Firestore TTL Guide - Auto-delete old documents to reduce storage costs
Google Cloud Functions:
- Google Cloud - Further Dive - Video tutorials on Pub/Sub, Cron jobs, Firestore Triggers
- Pub/Sub Events
- Scheduled Functions
- Firestore Trigger Events
- Cloud Storage Events
- Task Functions (Cloud Tasks)
References
Section titled “References”- Firebase Index Types Documentation
- Manage Indexes in Firestore
- Firestore Best Practices
- Too Many Index Entries for Entity - Common error when documents have too many indexed fields