Error
Error Code:
285
MongoDB Error 285: Index Build In Progress
Description
MongoDB Error 285, 'Index Build Already In Progress', indicates that an attempt was made to start a new index creation operation while another index build is still actively running on the same collection or database. This error prevents concurrent index builds to maintain data consistency and avoid resource contention. It typically occurs when a user or application tries to create another index before a previous one has completed.
Error Message
Index Build Already In Progress
Known Causes
3 known causesConcurrent Index Builds
An application or user initiated a new index creation command while another index build operation was still actively running on the same collection or database.
Unfinished Prior Build
A previous index build operation, possibly initiated earlier, is taking a long time to complete and is still active when a new build request arrives.
Automated Task Overlap
Multiple automated scripts or database management tools scheduled to create indexes have run simultaneously or too closely together.
Solutions
3 solutions available1. Wait for the Index Build to Complete easy
The simplest solution is to allow the ongoing index build to finish.
1
Monitor the index build progress. You can do this by querying the `system.indexes` collection or using `db.collection.getIndexes()`.
db.collection.getIndexes()
2
If the index build is consuming significant resources and impacting application performance, consider performing it during off-peak hours.
3
Once the index build is complete, subsequent operations that require this index will succeed.
2. Identify and Cancel the Existing Index Build medium
Locate and terminate the problematic index build if it's stuck or unwanted.
1
Connect to your MongoDB instance using the `mongo` shell or a driver.
2
Query the `system.profile` collection to find operations that are currently running. Look for operations related to index building.
db.system.profile.find({ op: 'command', 'command.createIndexes': { $exists: true } }).pretty()
3
Note the `_id` of the operation that is causing the 'Index Build Already In Progress' error. If you can't find it in the profile, you might need to inspect running operations more broadly.
4
Use the `killOp` command to terminate the identified operation. Replace `<op_id>` with the `_id` you found.
db.killOp(<op_id>)
5
After killing the operation, you can attempt to create the index again or investigate why the original build was stuck.
3. Use Background Index Builds easy
Create new indexes in the background to avoid blocking other operations.
1
When creating a new index, use the `background: true` option. This allows other database operations to continue while the index is being built.
db.collection.createIndex({ field: 1 }, { background: true })
2
Be aware that background index builds can still consume significant resources and might take longer to complete than foreground builds.
3
This is the recommended approach for creating indexes in production environments to minimize disruption.