How to fix Firestore: Query exceeds maximum 30 disjunctions in Firebase

FirebaseINTERMEDIATEMEDIUMUpdated Dec 22, 2025ยท 133 views

This error occurs when a Firestore query contains more than 30 OR conditions (disjunctions), exceeding Firestore's query complexity limit. To fix it, you need to restructure your query to use fewer disjunctions or implement alternative filtering approaches.

Quick fix

Fixes Firestore: Query exceeds maximum 30 disjunctions

// Problematic query with too many OR conditions
const query = db.collection('products')
  .where('category', 'in', [/* array with >30 items */]);

// OR multiple where clauses
const query = db.collection('users')
  .where('status', '==', 'active')
  .where('age', '>', 18)
  .where('country', 'in', [/* large array */]);
  1. Check your query for excessive OR conditions
  2. Split large `in` arrays into multiple queries
  3. Restructure complex OR logic
  4. Implement pagination or batch processing
firebase โ€” error
// Problematic query with too many OR conditions
const query = db.collection('products')
.where('category', 'in', [/* array with >30 items */]);
// OR multiple where clauses
const query = db.collection('users')
.where('status', '==', 'active')
.where('age', '>', 18)
.where('country', 'in', [/* large array */]);
Firestore: Query exceeds maximum 30 disjunctions

What this error means

Firestore has query complexity limits to ensure performance and scalability. One key limitation is the maximum number of disjunctions (OR conditions) allowed in a single query, which is 30. This limit applies to queries that use the `in` operator with arrays, multiple `where` clauses with OR logic, or complex compound queries. When you exceed this limit, Firestore rejects the query to prevent performance degradation and maintain service reliability for all users.

Typical symptoms

  • Query fails with "Firestore: Query exceeds maximum 30 disjunctions" error
  • Complex queries with many OR conditions stop working
  • Queries using large `in` arrays (more than 30 items) fail
  • Multiple `where` clauses combined with OR logic cause errors
  • Code that previously worked starts failing as data grows

Common causes

  1. Using an `in` operator with an array containing more than 30 items
  2. Chaining multiple `where` clauses with OR conditions exceeding 30 total
  3. Building dynamic queries that generate too many OR conditions based on user input
  4. Not accounting for query complexity when implementing search or filtering features
  5. Migrating from a database without similar query limitations

How to fix "Firestore: Query exceeds maximum 30 disjunctions"

1Check your query for excessive OR conditions

Examine your Firestore query to identify where disjunctions are being created. Look for:

javascript
// Problematic query with too many OR conditions
const query = db.collection('products')
  .where('category', 'in', [/* array with >30 items */]);

// OR multiple where clauses
const query = db.collection('users')
  .where('status', '==', 'active')
  .where('age', '>', 18)
  .where('country', 'in', [/* large array */]);

Count the total number of disjunctions in your query.

2Split large `in` arrays into multiple queries

If you're using the in operator with more than 30 items, split the array into chunks and combine results client-side:

javascript
// Split array into chunks of 30 or fewer
function chunkArray(array, chunkSize) {
  const chunks = [];
  for (let i = 0; i < array.length; i += chunkSize) {
    chunks.push(array.slice(i, i + chunkSize));
  }
  return chunks;
}

// Execute multiple queries
async function queryWithLargeInArray(items) {
  const chunks = chunkArray(items, 30);
  const promises = chunks.map(chunk => 
    db.collection('products')
      .where('id', 'in', chunk)
      .get()
  );
  
  const results = await Promise.all(promises);
  // Combine results
  return results.flatMap(snapshot => snapshot.docs);
}
3Restructure complex OR logic

For queries with many OR conditions, consider:

1. Use composite queries: Combine conditions where possible
2. Pre-filter data: Store pre-computed flags or categories
3. Use array-contains: Instead of multiple ORs, use array fields

javascript
// Instead of many OR conditions:
// .where('status', '==', 'pending')
// .where('status', '==', 'processing')
// .where('status', '==', 'shipped')

// Use array-contains with a statuses field
await db.collection('orders')
  .where('statuses', 'array-contains', 'active')
  .get();

// Or store active statuses in a separate array field
4Implement pagination or batch processing

For very large result sets, implement pagination to reduce query complexity:

javascript
// Paginate through results
async function getPaginatedResults(filters, pageSize = 20) {
  let query = db.collection('items');
  
  // Apply filters in batches
  for (const filter of filters.slice(0, 30)) {
    query = query.where(filter.field, filter.operator, filter.value);
  }
  
  const snapshot = await query.limit(pageSize).get();
  return snapshot.docs;
}

// Process remaining filters in subsequent pages if needed
5Use Firestore collection group queries strategically

For cross-collection queries, use collection group queries with careful filtering:

javascript
// Collection group query with limited disjunctions
const query = db.collectionGroup('items')
  .where('category', 'in', [/* max 30 items */])
  .where('active', '==', true);

// Combine with client-side filtering for additional criteria
const snapshot = await query.get();
const filtered = snapshot.docs.filter(doc => 
  // Additional client-side filtering
  doc.data().price > 100 && doc.data().inStock
);
6Consider alternative data modeling

If you consistently hit disjunction limits, reconsider your data model:

1. Denormalize data: Store computed values to reduce query complexity
2. Use subcollections: Break data into logical groupings
3. Implement materialized views: Pre-compute common query results
4. Use Cloud Functions: Run complex queries server-side where limits are different

Example of denormalization:

javascript
// Instead of querying across many fields
// Store a tags array for efficient querying
{
  title: "Product",
  tags: ["electronics", "sale", "featured", "in-stock"],
  // ... other fields
}

// Query becomes simpler
await db.collection('products')
  .where('tags', 'array-contains-any', [/* up to 10 items */])
  .get();

Advanced notes

Firestore's 30-disjunction limit is a hard constraint designed to maintain query performance. Note that array-contains-any also has a limit of 10 items. For extremely complex querying needs, consider:

1. Cloud Functions: Run complex queries in a server environment with different constraints
2. BigQuery export: Use Firestore's BigQuery export for analytical queries
3. Composite indexes: Carefully design composite indexes for common query patterns
4. Query planning: Use the Firestore query planner tool to analyze query complexity

Remember that client-side merging of multiple query results has trade-offs: increased latency, more document reads (cost), and potential consistency issues if data changes between queries.

Frequently asked questions

What does "Firestore: Query exceeds maximum 30 disjunctions" mean?
Firestore has query complexity limits to ensure performance and scalability. One key limitation is the maximum number of disjunctions (OR conditions) allowed in a single query, which is 30. This limit applies to queries that use the in operator with arrays, multiple where clauses with OR logic, or complex compound queries.
How do I fix "Firestore: Query exceeds maximum 30 disjunctions"?
This error occurs when a Firestore query contains more than 30 OR conditions (disjunctions), exceeding Firestore's query complexity limit. To fix it, you need to restructure your query to use fewer disjunctions or implement alternative filtering approaches.
What causes "Firestore: Query exceeds maximum 30 disjunctions"?
Using an in operator with an array containing more than 30 items Chaining multiple where clauses with OR conditions exceeding 30 total Building dynamic queries that generate too many OR conditions based on user input
Is the "Firestore: Query exceeds maximum 30 disjunctions" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading