MongoDB Timeout Query

1k Views Asked by At

I'm trying to figure out if there's a way to apply a query timeout in the script file of MongoDB to prevent slow/idle/hanging requests.

I am referring https://docs.mongodb.com/manual/reference/method/cursor.maxTimeMS/#examples documentation.

Now I am trying to write script as below for the maxTimeMS as below:-

#!/bin/bash
db.collection.find({ $query: {}, $maxTimeMS: 100 })

And it is giving an error:-

mongodb-timeout.sh: line 4: syntax error near unexpected token `{'
mongodb-timeout.sh: line 4: `db.collection.find({ $query: {}, $maxTimeMS: 100 })'

Does anybody have an idea? Thanks in advance

2

There are 2 best solutions below

1
On

I think maxTimeMS is the curser option, not a system-wide variable. As you can see in their jira you might be able to write some script to achieve this.

0
On

What you probably need, is mongodb Node.js driver documentation. There is an option to set a timeout for particular query (i.e. cursor).

cursor.maxTimeMS is a Mongo shell method which is different from Node.js (though both of them are javascript based).