How to fix CannotIndexParallelArrays: cannot index parallel arrays in MongoDB

MongoDBINTERMEDIATEMEDIUMUpdated Dec 22, 2025· 117 views

MongoDB prevents creating compound indexes on multiple array fields to avoid exponential index growth. This error occurs when you attempt to insert a document with arrays in multiple fields covered by a compound index.

Quick fix

Fixes CannotIndexParallelArrays: cannot index parallel arrays

// Check your existing compound index
db.collection.getIndexes()

// Example output showing compound index:
// { "Children.Name": 1, "Guardians.Name": 1 }

// Inspect a problematic document
db.collection.findOne()
  1. Identify which fields contain arrays
  2. Choose a restructuring strategy
  3. Update existing documents if needed
  4. Recreate indexes with the new structure
mongodb — error
// Check your existing compound index
db.collection.getIndexes()
// Example output showing compound index:
// { "Children.Name": 1, "Guardians.Name": 1 }
// Inspect a problematic document
db.collection.findOne()
CannotIndexParallelArrays: cannot index parallel arrays

What this error means

This error occurs when you try to create or use a compound multikey index where more than one indexed field contains an array value. MongoDB enforces a strict limitation: in any compound index, at most one field per document can be an array. The restriction exists for performance reasons. If MongoDB allowed indexing multiple array fields in a compound index, it would create a combinatorial explosion of index entries. For example, if you have a compound index on two fields and a document has arrays with 3 and 4 elements respectively, MongoDB would need to create 3×4=12 index entries for that single document. With larger arrays, this quickly becomes unmanageable. When you create a compound index like { a: 1, b: 1 }, MongoDB allows documents where field 'a' is an array OR field 'b' is an array, but not both. If you attempt to insert a document where both indexed fields are arrays, MongoDB rejects the operation with error code 171.

Typical symptoms

  • Insert or update operations fail with error code 171
  • Error message states "cannot index parallel arrays"
  • Compound index creation succeeds, but document insertion fails
  • Application throws CannotIndexParallelArrays exception during write operations

Common causes

  1. Attempting to insert a document where multiple fields in a compound index contain array values
  2. Creating a compound index on fields that already contain arrays in multiple fields of existing documents
  3. Schema design that requires indexing multiple array fields together without restructuring
  4. Migration or bulk import operations that violate the compound multikey index constraint

How to fix "CannotIndexParallelArrays: cannot index parallel arrays"

1Identify which fields contain arrays

First, examine your document structure and the compound index definition to identify which fields are arrays:

javascript
// Check your existing compound index
db.collection.getIndexes()

// Example output showing compound index:
// { "Children.Name": 1, "Guardians.Name": 1 }

// Inspect a problematic document
db.collection.findOne()

Look for fields that contain array values in the index specification. The error occurs when you try to insert a document where TWO OR MORE of these indexed fields are arrays.

2Choose a restructuring strategy

You have several options to resolve this limitation:

Option 1: Create separate single-field indexes
Instead of one compound index with multiple array fields, create individual indexes:

javascript
// Drop the problematic compound index
db.collection.dropIndex({ "Children.Name": 1, "Guardians.Name": 1 })

// Create separate indexes
db.collection.createIndex({ "Children.Name": 1 })
db.collection.createIndex({ "Guardians.Name": 1 })

Option 2: Embed arrays as objects
Combine related arrays into a single array of objects:

javascript
// Instead of parallel arrays:
{
  names: ["Alice", "Bob"],
  ages: [25, 30]
}

// Use a single array of objects:
{
  people: [
    { name: "Alice", age: 25 },
    { name: "Bob", age: 30 }
  ]
}

// Then create index on the embedded fields
db.collection.createIndex({ "people.name": 1, "people.age": 1 })

Option 3: Keep only one array field in the compound index
Mix scalar and array fields in your compound index:

javascript
// This is allowed (status is scalar, tags is array)
db.collection.createIndex({ status: 1, tags: 1 })

// Documents can have tags as array, but status must be scalar
{ status: "active", tags: ["urgent", "customer"] }
3Update existing documents if needed

If you're restructuring your data model, migrate existing documents to the new schema:

javascript
// Example: Converting parallel arrays to array of objects
db.collection.find({ names: { $exists: true } }).forEach(doc => {
  const people = doc.names.map((name, index) => ({
    name: name,
    age: doc.ages[index]
  }));

  db.collection.updateOne(
    { _id: doc._id },
    {
      $set: { people: people },
      $unset: { names: "", ages: "" }
    }
  );
});

Always backup your data before running migration scripts.

4Recreate indexes with the new structure

After restructuring your documents, create the appropriate indexes:

javascript
// For single array of objects approach
db.collection.createIndex({ "people.name": 1, "people.age": 1 })

// For separate indexes approach
db.collection.createIndex({ "field1": 1 })
db.collection.createIndex({ "field2": 1 })

// Verify indexes were created successfully
db.collection.getIndexes()

Test your queries to ensure they still perform well with the new index structure.

5Test document insertion

Verify that you can now insert documents without the parallel arrays error:

javascript
// Test with the new structure
db.collection.insertOne({
  people: [
    { name: "Alice", age: 25 },
    { name: "Bob", age: 30 }
  ],
  status: "active"
})

// Should succeed without CannotIndexParallelArrays error

Monitor your application logs to ensure the error no longer occurs during normal operations.

Advanced notes

Understanding Multikey Index Limitations

MongoDB's multikey indexes are powerful for querying array fields, but they come with specific constraints. Each entry in an array generates a separate index entry, which is why indexing multiple arrays would cause exponential growth.

Query Performance Considerations

When you split a compound index into separate single-field indexes, MongoDB can still use both indexes in a query through index intersection. However, this is generally less efficient than a true compound index. Consider your query patterns carefully:

- If you frequently query both fields together with equality conditions, restructuring data to use a single array of objects may provide better performance
- If queries typically filter on one field at a time, separate indexes work well
- Use the explain() method to analyze query performance after restructuring

Sharding Implications

In sharded clusters, this error can occur during chunk migration if documents being moved contain parallel arrays for a compound index. The shard key itself cannot contain arrays, but other indexed fields may trigger this error during internal operations.

Working with Nested Arrays

You can create compound indexes on nested fields within the same array without triggering this error:

javascript
// This is allowed - both fields are in the same array
db.collection.createIndex({
  "people.name": 1,
  "people.age": 1
})

// Documents like this work fine
{
  people: [
    { name: "Alice", age: 25 },
    { name: "Bob", age: 30 }
  ]
}

The key is that multiple index paths traverse the same array, not parallel separate arrays.

Frequently asked questions

What does "CannotIndexParallelArrays: cannot index parallel arrays" mean?
This error occurs when you try to create or use a compound multikey index where more than one indexed field contains an array value. MongoDB enforces a strict limitation: in any compound index, at most one field per document can be an array. The restriction exists for performance reasons.
How do I fix "CannotIndexParallelArrays: cannot index parallel arrays"?
MongoDB prevents creating compound indexes on multiple array fields to avoid exponential index growth. This error occurs when you attempt to insert a document with arrays in multiple fields covered by a compound index.
What causes "CannotIndexParallelArrays: cannot index parallel arrays"?
Attempting to insert a document where multiple fields in a compound index contain array values Creating a compound index on fields that already contain arrays in multiple fields of existing documents Schema design that requires indexing multiple array fields together without restructuring
Is the "CannotIndexParallelArrays: cannot index parallel arrays" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading