How to Fix MongoDB Error 366 (WouldChangeOwningShardDeletedNoDocument)
You see mongodb error 366 when a shard key update fails to delete the old document. You can fix it by retrying the transaction and checking concurrent delete operations. In MongoDB, code 366 maps to the internal name WouldChangeOwningShardDeletedNoDocument. This failure happens inside sharded clusters when an update changes a document's shard key value. When a shard key changes, the database must move that document to a new shard. If the origin shard cannot remove the original record, the clust
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 see mongodb error 366 when a shard key update fails to delete the old document. You can fix it by retrying the transaction and checking concurrent delete operations.
In MongoDB, code 366 maps to the internal name WouldChangeOwningShardDeletedNoDocument. This failure happens inside sharded clusters when an update changes a document's shard key value. When a shard key changes, the database must move that document to a new shard. If the origin shard cannot remove the original record, the cluster stops the operation.
What causes mongodb error 366 in sharded clusters
A sharded cluster splits records across multiple servers based on a chosen shard key. Most updates stay on one shard because the shard key value stays the same. Sometimes an app updates the shard key itself, which changes the owning shard. Behind the scenes, the query router runs this change as a multi-shard transaction. It inserts the updated document on the target shard and deletes the original document. If the delete step finds zero matching documents, the engine returns error 366.
Race conditions are the most common cause for this missing document problem. Two app threads might try to change or remove the same record at once. The first thread deletes the record or moves it to another shard first. When the second thread tries to delete the old record, nothing is left there. Rather than risk duplicate data across shards, the database aborts the whole transaction.
Background chunk migrations or orphan cleanups can also trigger this state under heavy write loads. When chunks move between shards, document ownership shifts across the cluster nodes. If an app sends an uncoordinated write during this window, the delete step can miss. Using a retry loop in your app logic handles these rare timing conflicts well.
How to diagnose and fix the failure in production
Start by checking your app logs to find which query triggered the shard key change. You do not need admin rights on the whole cluster to inspect this problem. MongoDB grants access through role-based rules down to the single collection level. The standard read role lets engineers run find and listIndexes on non-system collections. This read role does not allow insert, update, remove, or dropCollection actions.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Use a read role account to check if the target document already moved shards. Run a query by the unique record ID rather than the old shard key. If the document exists with the new shard key, another worker finished the update. If the document is gone, a separate delete request removed it before your update finished.
When an engineer must fix stuck records by hand, scope their permissions with care. The readWrite role adds insert, update, and remove actions on a single database. Grant this role only on the specific collection that holds the broken shard records. Also check if your data masking rules rewrite emails or phone numbers in query results. If a script reads a masked email and writes it back, the shard key changes.
To prevent future errors, avoid changing shard key fields in high-traffic app paths. If your app must update shard keys, wrap the logic in a transaction retry block. Make sure only one worker processes a given record ID at any moment. This simple design stops two workers from fighting over the same document across shards.
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 WouldChangeOwningShardDeletedNoDocument mean in MongoDB?
It means an update tried to move a document to a new shard. The cluster aborted the move because the original document was already gone during deletion.
Is data lost when mongodb error 366 happens?
No data is lost because shard key updates run inside a transaction. When error 366 triggers, the cluster rolls back the partial write on the target shard.
Which database role do I need to inspect collections after error 366?
The built-in read role is enough to run find and collStats on the affected database. You only need the readWrite role if you must manually update or remove records.
Related guides
In this post