How to Fix MongoDB Error 359 (CannotConvertIndexToUnique)
You get mongodb error 359 when a database index conversion to unique fails. Fix it by removing duplicate values or building a fresh unique index. The official server name for this fault is CannotConvertIndexToUnique. Database teams see this code during schema changes on live collections. When a collection holds bad data or conflicting rules, the operation stops right away. What causes mongodb error 359 in your database In the database engine, a standard index allows many documents to share v
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 359 when a database index conversion to unique fails. Fix it by removing duplicate values or building a fresh unique index.
The official server name for this fault is CannotConvertIndexToUnique. Database teams see this code during schema changes on live collections. When a collection holds bad data or conflicting rules, the operation stops right away.
What causes mongodb error 359 in your database
In the database engine, a standard index allows many documents to share values. A unique index forces every document to hold a distinct value for that key. When you ask the server to convert an existing index, it checks every record. If two documents share the same key value, the conversion fails with code 359.
Missing fields can also trigger this index conversion error during a deployment. The database treats a missing field as a null value inside the index. If two documents lack the indexed field, both get a null entry. That second null entry breaks uniqueness and stops the index change.
Index type conflicts can cause CannotConvertIndexToUnique as well. Not every index structure supports a unique rule in the storage engine. For instance, hashed indexes and certain text indexes cannot enforce unique values. Trying to convert an incompatible index type will return this exact error code.
How to diagnose and fix the failure in production
Start your diagnosis with the least privilege needed to inspect the collection. MongoDB grants access through roles that apply down to the collection level. Give the engineer the built-in read role on the target database first. The read role allows find and listIndexes actions without letting users change data.
Use those read permissions to list the current indexes on the collection. Check the index key fields and see if other options are active. Next, run an aggregation query to group documents by the target index keys. Count the records in each group to spot any duplicate values or multiple nulls.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Before you inspect results from user collections, think about data privacy. Indexed fields often hold sensitive strings like emails, phone numbers, or national IDs. Apply data masking so the system rewrites matched fields in query results. This lets engineers find duplicate patterns while keeping sensitive personal details masked.
Once you find the duplicate documents, plan a safe cleanup step. Do not grant full cluster admin rights to fix a single collection. The built-in readWrite role includes update, remove, createIndex, and dropIndex actions. Scope that role to the specific database and collection only for the repair window.
Merge or remove the duplicate records so every key value is unique. If the engine blocks in-place conversion, build a new unique index alongside the old one. Wait for the new index build to finish, then drop the old non-unique index. This approach avoids downtime and keeps query performance steady during the fix.
Add command guardrails and field masking beside MongoDB
Collection roles and IP rules control who connects to MongoDB. Yet an active client or agent session can still run a pipeline that writes data or reads raw personal fields. In practice, hoop.dev sits inline in the wire protocol beside your database as a sidecar for your MongoDB. For every command an agent or human sends in production, hoop.dev decides to pass, mask, block, or escalate it for review.
Using hoop.dev MongoDB controls, guardrails refuse banned commands before they run or hold risky calls for approval, and data masking rewrites matched fields in returned documents in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and generates evidence for GDPR, HIPAA, and PCI DSS programs. View the project on GitHub.
Frequently asked questions
What does CannotConvertIndexToUnique mean in MongoDB?
CannotConvertIndexToUnique is the official name for error code 359 in the database server. It means the engine could not turn an existing index into a unique index.
Which role can check indexes without changing production data?
The built-in read role lets users run find and listIndexes on a database. It blocks insert, update, remove, and drop actions so production data stays safe.
Can two documents with missing fields cause error 359?
Yes, the index stores a missing field as a single null value. Two documents without that field create duplicate null entries and block unique conversion.
Related guides
In this post