Error
Error Code:
43
MongoDB Error 43: Cursor Expired or Invalid
Description
This error indicates that MongoDB cannot find or access a requested database cursor. It typically occurs when a client application attempts to retrieve more data from a cursor that has either timed out due to inactivity, been explicitly closed, or is no longer valid on the server side.
Error Message
Cursor Not Found
Known Causes
3 known causesCursor Timeout
MongoDB cursors automatically close after a period of inactivity (default 10 minutes) to free up server resources, making subsequent attempts to use them fail.
Client Cursor Closure
The client application attempted to use a cursor that was already explicitly closed, or from which all available documents had already been retrieved.
Server Restart or Failover
A MongoDB server restart, replica set primary failover, or sharding rebalancing can invalidate active cursors on the server, leading to this error.
Solutions
3 solutions available1. Increase Cursor Timeout for Long-Running Operations medium
Adjust the cursor timeout setting to prevent cursors from expiring during extended query execution.
1
Connect to your MongoDB instance using the mongo shell or a driver.
2
Set the `cursorTimeoutMillis` option for your query. This value is in milliseconds. A common starting point is to increase it significantly, e.g., to 10 minutes (600000 ms) or more, depending on your expected operation duration.
db.collection.find().maxTimeMS(600000)
3
Alternatively, if you are using a driver, consult its documentation for how to set cursor timeout options for your specific language. For example, in PyMongo, you would pass `max_time_ms` to the `find` method.
cursor = collection.find(max_time_ms=600000)
4
For a more persistent solution, you can configure the `cursorTimeoutMillis` server-side. This is usually done in the `mongod.conf` file. Restart the MongoDB server after making changes.
storage:
dbPath: /var/lib/mongodb
systemLog:
destination: file
path: /var/log/mongodb/mongod.log
logAppend: true
net:
bindIp: 127.0.0.1
port: 27017
# Add or modify this line for cursor timeout
operationProfiling:
slowOperationThresholdMs: 100
# This setting is often controlled via command-line or specific driver options
# rather than directly in mongod.conf for cursor timeout itself.
# However, for general operation timeouts, consider `maxTimeMS` in queries.
2. Process Cursors in Batches medium
Retrieve and process data from cursors in smaller, manageable chunks to avoid timeouts.
1
When fetching data, use methods that allow you to retrieve documents in batches. The `limit` and `skip` operations can be used to paginate through results.
batchSize = 1000
page = 0
while True:
documents = db.collection.find().skip(page * batchSize).limit(batchSize)
if not documents.count():
break
for doc in documents:
# Process document
pass
page += 1
2
Alternatively, if your driver supports it, use cursor batching options. For example, in PyMongo, you can set `batch_size`.
cursor = collection.find(batch_size=1000)
for doc in cursor:
# Process document
pass
3
Ensure that the processing logic for each batch is efficient and completes well within the cursor's timeout period.
3. Check Server Load and Network Latency medium
Investigate if high server load or network issues are causing delays that lead to cursor expiration.
1
Monitor your MongoDB server's performance metrics, including CPU usage, memory usage, disk I/O, and network traffic. Use tools like `mongotop`, `mongostat`, or MongoDB Atlas's performance dashboards.
mongostat --host <your_mongodb_host> --port <your_mongodb_port> 1
2
Check for any long-running queries or operations that might be consuming excessive resources and slowing down the server.
db.currentOp()
3
Test network connectivity between your application server and the MongoDB server. High latency or packet loss can significantly impact query response times.
ping <your_mongodb_host>
4
If server load is high, consider optimizing queries, adding indexes, or scaling your MongoDB deployment (e.g., by adding more nodes to a replica set or sharded cluster).