How to Fix MongoDB Error 86 (IndexKeySpecsConflict)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get mongodb error 86 when an index creation command clashes with an existing index. To fix this IndexKeySpecsConflict error, match the existing index keys or drop the old index first. Database teams often see this failure during app deploys or schema migrations. A script tries to build an index using a name that already exists. If the key fields or sort orders differ, the database stops the operation. This safety check protects your existing query plans from sudden changes. What causes mo

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You get mongodb error 86 when an index creation command clashes with an existing index. To fix this IndexKeySpecsConflict error, match the existing index keys or drop the old index first.

Database teams often see this failure during app deploys or schema migrations. A script tries to build an index using a name that already exists. If the key fields or sort orders differ, the database stops the operation. This safety check protects your existing query plans from sudden changes.

What causes mongodb error 86 in your database

In the official error catalog, code 86 carries the name IndexKeySpecsConflict. The database engine tracks every index on a collection by its name and key pattern. When an app sends an index build request, the engine checks the collection catalog first. If no index shares that name, the engine creates the new index right away.

If an index with the same name already exists, the engine compares both key patterns. When the keys and sort directions match, the database treats the request as a safe no-op. When the key fields or sort directions differ, the engine raises mongodb error 86. It refuses to overwrite the old index behind the scenes.

This conflict happens for a few common reasons in live clusters. Developers might change the sort order of a field in code without changing the index name. Two microservices might share one collection and define different keys under the same default name. Automated migration tools can also fail when someone edits an old migration file after it ran. Replication lag or failed rollbacks during a deploy can leave half-updated index definitions across environments.

How to diagnose and fix IndexKeySpecsConflict in production

Fixing an index conflict in production requires care so you do not break live queries. Do not grant full admin rights to every engineer just to inspect a broken deploy. MongoDB grants access through roles that apply down to a single collection. The built-in read role lets an engineer list indexes and check collection stats safely. This role does not allow anyone to insert, update, remove data, or drop collections.

Start the check by using a read role account to list all indexes on the target collection. Compare the existing index keys in the database against the keys in your deployment script. Look closely at field names, field order, and ascending or descending sort values. Even one flipped sort direction will trigger code 86 if the index name stays the same.

MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Once you find the mismatch, you have three safe ways to fix the problem. First, if the existing index in production is right, update your app code to match it. Second, if your app needs both index shapes, give the new index a fresh name. Third, if the old index is obsolete, drop it and create the new one.

Dropping and creating indexes requires higher permissions than the basic read role provides. The built-in readWrite role includes actions to create and drop indexes on a database. Run the drop and create steps during low traffic hours to avoid heavy disk load. Better yet, build the new index under a new name before you drop the old one. This swap method keeps queries fast throughout the entire change.

Preventing index conflicts across deployment pipelines

You can stop index key conflicts before they ever reach your production database. Store all index definitions in version control so your team reviews every change. Avoid using custom index names unless your app logic needs them for query hints. When the database generates index names from the keys, a key change creates a new name. That simple rule prevents most name collisions during schema updates.

Disable automatic index builds on app startup in your production clusters. Instead, run index changes as controlled migration steps with proper peer review. Check staging databases before each release to confirm index catalogs match production.

Check MongoDB commands and documents on the wire

MongoDB roles and SCRAM login decide who opens a session. They do not check single commands or redact sensitive fields inside returned documents. With hoop.dev, you run an open-source proxy, a sidecar for your MongoDB, inline in the MongoDB wire protocol. On every command an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review.

Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13, or hold risky commands for approval. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents in memory, and every verdict is logged. See the open-source code on GitHub.

Frequently asked questions

What does IndexKeySpecsConflict mean in plain terms?

It means you tried to create an index using a name that is already taken. The existing index uses different fields or sort orders than your new command.

Which role can inspect indexes without changing production data?

The built-in read role lets a user list indexes and check collection stats safely. It blocks the user from dropping indexes or changing any documents.

Does dropping an index cause downtime for live apps?

Dropping an active index can make live queries run much slower right away. Always build the replacement index under a new name before dropping the old one.

The query runs. The emails stay hidden.