How to Fix MongoDB Error 96 (OperationFailed)
You see mongodb error 96 when a database query fails or loses its active cursor. To fix this OperationFailed error, add sort indexes, lower batch sizes, or reopen the cursor. In the official database rules, code 96 maps to the OperationFailed name. It also belongs to the CursorInvalidatedError group. This means the engine stopped a running read or write task before completion. You will often see this during large data sorts or long collection scans. What causes mongodb error 96 in your cluste
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 96 when a database query fails or loses its active cursor. To fix this OperationFailed error, add sort indexes, lower batch sizes, or reopen the cursor.
In the official database rules, code 96 maps to the OperationFailed name. It also belongs to the CursorInvalidatedError group. This means the engine stopped a running read or write task before completion. You will often see this during large data sorts or long collection scans.
What causes mongodb error 96 in your cluster
Several database engine limits can trigger mongodb error 96 during daily work. The most common cause is an unindexed sort that hits memory limits. When a query sorts many documents without an index, the engine uses system RAM. If that sort uses too much memory, the server aborts the task right away.
Another frequent trigger is an invalid or closed cursor on the server side. Cursors hold your place while an app reads through a large set of documents. If a collection drops or a shard moves data, the active cursor becomes invalid. The database then returns code 96 to tell the client to stop reading.
Network drops and storage timeouts also cause this failure in busy clusters. Long queries that wait for disk locks may pass their allowed run time. When the engine kills the slow task, the client driver receives an OperationFailed response.
How to diagnose and fix the failure safely
Start by checking your database server logs for the exact query that failed. The log message next to code 96 shows why the engine stopped the task. Look for notes about sort memory limits or dropped collection states.
To check query performance safely, use a database account with minimal permissions. MongoDB grants access through role-based rules down to the collection level. Give the reviewer the built-in read role on that target database. This role allows find, listCollections, listIndexes, dbStats, and collStats actions without allowing data changes.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
If the log points to a large sort, check the indexes on that collection. You need an index that matches the fields used in your sort step. Adding the right index lets the engine read data in order without heavy memory use.
Only grant the readWrite role when an engineer must create or drop an index. The readWrite role includes everything in the read role plus write actions. It allows createIndex and dropIndex, along with insert, update, remove, and dropCollection. Limiting who holds this role prevents accidental data loss during an incident.
How to prevent cursor failures in client apps
You can also change how your app reads data to avoid cursor errors. Keep cursor batch sizes small so each network trip finishes fast. Do not leave cursors idle for long periods between read calls.
If your app uses change streams, build retry logic into the data reader. The read role includes the changeStream action for watching data updates. When a cursor fails with code 96, your app should catch the error and start a fresh cursor.
Finally, protect private user fields when engineers run test queries in production. Data masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in query results. This lets your team inspect broken queries and fix sort errors safely.
Inspect MongoDB wire traffic in flight
Built-in MongoDB roles govern collection access at setup time. They cannot score command risk per call or mask sensitive document values on the fly. By design, hoop.dev sits inline in the MongoDB protocol next to your server as a sidecar for your MongoDB. On every production command from mongosh, drivers, or AI agents, hoop.dev decides whether to pass, mask, block, or hold it for human review.
First, hoop.dev guardrails block bad deletes, drops, and write pipelines or hold them for approval. Next, hoop.dev data masking rewrites sensitive values in returned documents in memory and logs each decision. Explore the repo on GitHub.
Frequently asked questions
What does OperationFailed mean for code 96?
OperationFailed is the official name for code 96 in the database engine. It belongs to the CursorInvalidatedError category and means a running task or cursor stopped early.
Can missing indexes cause mongodb error 96?
Yes, sorting a large collection without a matching index uses a lot of RAM. When the sort passes the memory limit, the database aborts the query with code 96.
Which role do I need to inspect indexes for this error?
The built-in read role lets you run listIndexes, find, and collStats on non-system collections. You only need the readWrite role if you must run createIndex to fix the query.
Related guides
In this post