Cursors and time limits
Go through large results in batches and put a time limit on queries.
Go through large results in batches with a cursor and put a time limit on each query with maxTimeMS.
Requirements
- A connection with a user that has
readon the database.
Go through in batches
JavaScript
const cursor = db.orders.find({ status: "paid" }).batchSize(500);
for await (const order of cursor) {
handle(order);
}The driver asks for each next batch with getMore to the same node that opened the cursor.
Put a time limit
JavaScript
db.orders.find({ status: "paid" }).maxTimeMS(2000);Check
A query over the time limit fails with MaxTimeMSExpired (50) and stops keeping the node busy.
Common problems
getMorefails withCursorNotFound(43) because the cursor expired, was closed or was asked of another node.