Error
Error Code:
237
MongoDB Error 237: Cursor Killed
Description
This error indicates that a MongoDB cursor, used to retrieve query results, has been prematurely closed or invalidated. It commonly occurs when the client application disconnects, the cursor times out due to inactivity, or the cursor is explicitly closed by the application or server.
Error Message
Cursor Killed
Known Causes
3 known causesClient Disconnection
The client application that initiated the query disconnected from the MongoDB server before all results were retrieved, leading the server to kill the associated cursor.
Cursor Timeout
The MongoDB server automatically closed the cursor because it remained idle for too long, exceeding the configured `cursorTimeoutMillis` without the client requesting the next batch of results.
Application-Initiated Closure
The client application explicitly closed the cursor object or the cursor went out of scope, prompting the server to kill the corresponding resource.
Solutions
3 solutions available1. Increase Cursor Timeout easy
Adjust the server-side cursor timeout to prevent cursors from expiring prematurely.
1
Connect to your MongoDB instance using the `mongo` shell or a MongoDB GUI tool.
2
Run the following command to increase the cursor timeout. The value is in seconds. A common starting point is 600 seconds (10 minutes).
db.adminCommand({ setParameter: 1, cursorTimeoutMillis: 600000 })
3
Verify the setting has been applied by checking the server configuration.
db.adminCommand({ getParameter: 1, 'cursorTimeoutMillis': 1 })
2. Optimize Application Logic for Cursors medium
Refactor your application to fetch data in smaller batches or process it more efficiently to avoid long-running cursors.
1
Review your application code that interacts with MongoDB cursors. Identify queries that are taking a long time to process or are not iterating through the cursor promptly.
2
Implement batching for large result sets. Instead of fetching all documents at once, fetch them in chunks using `limit()` and `skip()` or by adding a secondary query condition that advances based on the last processed document's ID or timestamp.
// Example using limit and skip (less efficient for very large datasets)
const batchSize = 100;
let skipCount = 0;
while (true) {
const documents = db.collection.find().limit(batchSize).skip(skipCount).toArray();
if (documents.length === 0) break;
// Process documents
skipCount += batchSize;
}
3
Ensure that your application actively iterates through the cursor. If the cursor is left idle for too long without fetching documents, it can expire.
// Ensure you are calling .next() or iterating over the cursor in a loop
const cursor = db.collection.find();
while (cursor.hasNext()) {
const doc = cursor.next();
// Process doc
}
4
Consider using aggregation pipelines with `.allowDiskUse()` if processing large datasets that might exceed memory limits, as this can sometimes lead to more efficient processing and fewer cursor issues.
db.collection.aggregate([
// ... pipeline stages ...
], { allowDiskUse: true })
3. Monitor and Manage Active Cursors medium
Use MongoDB's built-in tools to identify and terminate stale or problematic cursors.
1
Connect to your MongoDB instance using the `mongo` shell.
2
List all active cursors and their details.
db.listCursors()
3
Examine the output for cursors that have been open for an unusually long time or are associated with queries that appear to be stuck or inefficient. Look at the `created_at` and `last_use` fields.
4
If you identify a problematic cursor, you can kill it using its `cursorId`.
db.killCursors(<cursorId>)
5
For more proactive monitoring, consider using MongoDB Atlas's Performance Advisor or custom scripts to periodically check `db.listCursors()` and alert on long-lived cursors.