The cursor was killed by another operation
This error indicates that a cursor was explicitly killed by a command like `killCursors` before the client finished iterating through it. This is different from a cursor timing out due to inactivity; it is an intentional termination.
- 1An administrator manually running the `killCursors` command on the cursor's ID
- 2An application or script designed to clean up long-running queries killing the cursor
- 3In a sharded cluster, a `mongos` may kill a cursor on a shard if the client connection to the `mongos` is lost
An administrator identifies a long-running query and manually kills its cursor while a client is still processing it.
// In Shell 1, create a cursor and pause.
const cursor = db.largeCollection.find();
cursor.next(); // Get first batch
// ... client pauses ...
// In Shell 2, find the cursor ID and kill it.
const ops = db.currentOp();
const op = ops.inprog.find(op => op.cursor && op.cursor.cursorId);
if (op) {
db.killCursors("largeCollection", [op.cursor.cursorId]);
}
// When Shell 1 resumes, it gets an error.
cursor.next();expected output
MongoServerError: cursor killed
Fix 1
Investigate the Source of `killCursors`
WHEN This error appears unexpectedly.
Why this works
Determine why the cursor is being killed. Is it a manual intervention? An automated script? Understanding the source is key. Check the `currentOp` command and server logs for clues.
Fix 2
Make Client Code Resilient
WHEN Cursors can be killed as part of normal cluster operations.
Why this works
If the operation is idempotent, the client can be designed to re-execute the query upon receiving a `CursorKilled` error. This makes it resilient to administrative cleanup or certain failover scenarios.
✕ Run `killCursors` indiscriminately on any long-running query
This can disrupt legitimate application processes. It is better to optimize the query or work with the application team to understand its behavior before killing its operations.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev