Error
Error Code: 85

MongoDB Error 85: Index Options Conflict

📦 MongoDB
📋

Description

This error occurs when attempting to create or modify an index with options that conflict with an existing index definition or the data itself. It indicates an incompatibility in the specified index properties, preventing the operation from completing successfully.
💬

Error Message

Index Options Conflict
🔍

Known Causes

4 known causes
⚠️
Conflicting Index Options
Specifying options (e.g., unique, sparse, collation) for a new index that contradict an existing index on the same fields.
⚠️
Data Incompatibility
Attempting to create a unique index on a collection that already contains duplicate values for the indexed field(s).
⚠️
Mutually Exclusive Options
Providing incompatible or mutually exclusive options within the same index creation command (e.g., trying to combine conflicting partial filter expressions).
⚠️
Recreating with Different Options
Attempting to create a new index with different options on an existing key pattern without explicitly dropping the original index first.
🛠️

Solutions

3 solutions available

1. Identify and Remove Conflicting Index Definitions medium

Find and drop duplicate or conflicting index definitions on a collection.

1
Connect to your MongoDB instance using the `mongosh` shell.
mongosh
2
Switch to the database containing the collection with the error.
use your_database_name
3
List all existing indexes on the problematic collection to identify duplicates or conflicting options.
db.your_collection_name.getIndexes()
4
Carefully examine the output from `getIndexes()`. Look for indexes that are identical in terms of fields and order but have different options (e.g., `unique: true` vs. `unique: false`, different `background` settings, or different `sparse` settings). Also, look for indexes with the same key pattern but different collation settings.
text
5
If you find a conflicting index, drop it using its name. Replace 'index_name' with the actual name of the index you want to drop.
db.your_collection_name.dropIndex('index_name')
6
After dropping the conflicting index, attempt to create the intended index again.
db.your_collection_name.createIndex({ field1: 1, field2: -1 }, { unique: true, background: true })

2. Recreate Index with Correct Options easy

Drop the existing index and recreate it with the desired, non-conflicting options.

1
Connect to your MongoDB instance using the `mongosh` shell.
mongosh
2
Switch to the database containing the collection.
use your_database_name
3
Identify the name of the index causing the conflict. You can do this by inspecting the error message or by using `db.your_collection_name.getIndexes()`.
db.your_collection_name.getIndexes()
4
Drop the existing index. Replace 'index_name' with the actual name of the index.
db.your_collection_name.dropIndex('index_name')
5
Create the index again with the correct, non-conflicting options. Ensure you are not trying to create an index with the same key pattern but different options (e.g., trying to create a unique index where a non-unique one already exists or vice-versa).
db.your_collection_name.createIndex({ field1: 1, field2: -1 }, { unique: true, background: true, collation: { locale: 'en', strength: 1 } })

3. Address Collation Mismatches medium

Ensure that indexes with the same key pattern have consistent collation settings.

1
Connect to your MongoDB instance using the `mongosh` shell.
mongosh
2
Switch to the database containing the collection.
use your_database_name
3
List all indexes on the collection and pay close attention to the `collation` field for each index.
db.your_collection_name.getIndexes()
4
Identify if multiple indexes exist on the same key pattern but with different `collation` settings. For example, one might have a default collation and another might specify a custom one.
text
5
If a conflict is found due to collation, drop the conflicting index. You might need to decide which collation setting is appropriate for your application.
db.your_collection_name.dropIndex('index_name')
6
Recreate the index with the desired and consistent collation. If you want the default collation, omit the `collation` option. If you need a specific collation, define it explicitly.
db.your_collection_name.createIndex({ field: 1 }, { collation: { locale: 'en', strength: 2 } })
🔗

Related Errors

5 related errors