Reputation: 1078
I want to terminate my find query if that query cross time limit so I using a query like this :
db.collection.find({description: /August [0-9]+, 1969/}).maxTimeMS(50).toArray()
According to this, if my find query takes more than 50 milliseconds, then I want to terminate this query. but I need to understand what happens at the DB level if the query crosses the 5 milliseconds then my application giving me the error but on the DB level the query still running or it also terminates or it will complete and then terminate?
Kindly help me here to understand this and if possible how we can check this?
Upvotes: 1
Views: 2913
Reputation: 57095
Yes, MongoDB will kill the operation.
MongoDB targets operations for termination if the associated cursor exceeds its allotted time limit. MongoDB terminates operations that exceed their allotted time limit using the same mechanism as db.killOp(). MongoDB only terminates an operation at one of its designated interrupt points.
MongoDB does not count network latency between the client and the server towards a cursor's time limit. For a sharded cluster, however, MongoDB does include the latency between the mongos and mongod instances towards this time limit.
Queries that generate multiple batches of results continue to return batches until the cursor exceeds its allotted time limit.
Read maxTimeMS
From - https://github.com/Automattic/mongoose/issues/4066#issuecomment-502260563
https://mongoosejs.com/docs/api/mongoose.html#mongoose_Mongoose-set
mongoose.set('maxTimeMS', 3000);
Or:
https://mongoosejs.com/docs/api/connection.html#connection_Connection-set
const db = mongoose.createConnection(uri);
db.set('maxTimeMS', 3000);
Upvotes: 3