Error
Error Code:
67
MongoDB Error 67: Cannot Create Index
Description
MongoDB Error 67 indicates a failure to create an index on a collection. This typically occurs when there are issues with the index definition, user permissions, or system resources.
Error Message
Cannot Create Index
Known Causes
4 known causesInvalid Index Definition
The index specification contains incorrect field names, unsupported options, or syntax errors.
Insufficient User Privileges
The authenticated user lacks the required `createIndex` privilege for the target database or collection.
Resource Limitations
The MongoDB server or underlying system has insufficient disk space, memory, or other resources to create the index.
Duplicate Index or Name
An index with the same name or an identical key pattern already exists on the collection.
Solutions
4 solutions available1. Verify Index Key Limits easy
Check if the index you're trying to create exceeds MongoDB's key size or document size limits.
1
Review the MongoDB documentation for current index key limits. These can change between versions.
https://www.mongodb.com/docs/manual/core/index-limits/
2
Examine the fields you are including in your index. If you are indexing large text fields or arrays, this can lead to exceeding limits. Consider indexing only the necessary parts of the fields, or using a different indexing strategy.
db.collection.createIndex( { field: 1, anotherField: 1 } )
3
If you are creating a compound index, ensure the total size of the keys does not exceed the limit. MongoDB has a limit on the total size of keys in an index.
db.collection.createIndex( { field1: 1, field2: 1, field3: 1 } )
4
If the index is too large, consider creating a partial index or indexing only a subset of the data.
db.collection.createIndex( { field: 1 }, { partialFilterExpression: { status: "active" } } )
2. Check Disk Space and Memory medium
Ensure your MongoDB server has sufficient free disk space and memory to create the index.
1
Check the available disk space on the server where your MongoDB data files are located. Index creation can consume significant disk space, especially for large collections.
df -h
2
Monitor the memory usage of your MongoDB process. Insufficient memory can lead to performance issues and failures during index creation.
top
3
If disk space is low, free up space by deleting old data, log files, or unused files. If memory is constrained, consider increasing the server's RAM or optimizing MongoDB's memory allocation.
text
3. Address Duplicate Keys in Unique Indexes medium
If you are attempting to create a unique index, ensure there are no existing duplicate values in the indexed field(s).
1
Before creating a unique index, query your collection to find any duplicate values in the field(s) you intend to index.
db.collection.aggregate([
{ $group: { _id: "$fieldToCreateIndexOn", count: { $sum: 1 } } },
{ $match: { count: { $gt: 1 } } }
])
2
If duplicates are found, you must either remove them or adjust the data to ensure uniqueness before creating the unique index.
db.collection.remove( { fieldToCreateIndexOn: <duplicate_value> } ) // Use with caution, consider backups
3
Once duplicates are resolved, you can attempt to create the unique index again.
db.collection.createIndex( { fieldToCreateIndexOn: 1 }, { unique: true } )
4. Rebuild or Repair the Database advanced
In rare cases, database corruption can cause index creation failures. Consider rebuilding or repairing the database.
1
Stop the MongoDB server gracefully.
mongod --shutdown
2
Navigate to your MongoDB data directory.
cd /var/lib/mongodb
3
Run the `repairDatabase` command. This can take a significant amount of time and may involve data loss if the corruption is severe. It's highly recommended to have a backup before proceeding.
mongod --repair
4
Alternatively, you can consider a `rebuildIndex` operation if you suspect only a specific index is corrupted. This is less disruptive than `repairDatabase`.
db.collection.reIndex()
5
Start the MongoDB server again and attempt to create the index.
mongod