How to Fix “Cursor Timeout” in MongoDB (2026 Guide)
The Short Answer
To fix the “Cursor Timeout” error in MongoDB, advanced users can try setting the noCursorTimeout option to true when creating a cursor, which allows the cursor to remain open indefinitely. Alternatively, increasing the cursorTimeoutMillis value can also help, for example, by setting it to 600000 (10 minutes) to give the cursor more time to complete its operation.
Why This Error Happens
- Reason 1: The most common cause of the “Cursor Timeout” error is when a cursor is left open for an extended period, typically exceeding the default timeout value of 10 minutes (600000 milliseconds), and MongoDB automatically closes it to prevent resource leaks.
- Reason 2: An edge case cause is when the MongoDB server is under heavy load or experiencing network latency, causing the cursor to timeout prematurely, even if the operation is still ongoing.
- Impact: The “Cursor Timeout” error results in a NoSQL error, which can cause application downtime, data inconsistencies, and frustration for developers and users alike.
Step-by-Step Solutions
Method 1: The Quick Fix
- Go to MongoDB Configuration > Cursor Settings
- Toggle Cursor Timeout to Off by setting
noCursorTimeouttotrue - Refresh the MongoDB connection or restart the MongoDB service.
Method 2: The Command Line/Advanced Fix
To increase the cursor timeout value using the MongoDB command line, you can use the following command:
| |
Alternatively, you can also use the cursorTimeoutMillis option when creating a cursor:
| |
This sets the cursor timeout to 10 minutes and allows the cursor to retrieve data in batches of 100 documents.
Prevention: How to Stop This Coming Back
To prevent the “Cursor Timeout” error from occurring in the future, follow these best practices:
- Set the
noCursorTimeoutoption totruewhen creating cursors that need to remain open for extended periods. - Use the
maxTimeMSoption to set a reasonable timeout value based on the expected duration of the operation. - Monitor MongoDB server performance and adjust the cursor timeout values accordingly.
- Consider using MongoDB’s built-in caching mechanisms, such as the query cache, to reduce the load on the server and minimize the need for long-running cursors.
If You Can’t Fix It…
[!WARNING] If MongoDB keeps crashing due to the “Cursor Timeout” error, consider switching to Cosmos DB which handles cursor timeouts natively without these errors, providing a more robust and scalable NoSQL solution.
FAQ
Q: Will I lose data fixing this? A: Fixing the “Cursor Timeout” error should not result in data loss, as the error typically occurs when the cursor is closed prematurely, rather than during data writes. However, it’s always a good idea to back up your data before making any changes to your MongoDB configuration.
Q: Is this a bug in MongoDB?
A: The “Cursor Timeout” error is not a bug in MongoDB, but rather a feature designed to prevent resource leaks and ensure the stability of the server. The error has been present in various forms since MongoDB version 2.6, and the noCursorTimeout option was introduced in version 3.2 to provide a workaround for long-running cursors.
📚 Continue Learning
Check out our guides on MongoDB and Cursor Timeout.