I'm writing a web application that uses stores. If the client doesn't receive a response within 30 seconds, it considers the request dead and gives a timeout error.
I'm trying to get MongoDB to do the same thing. If for example the connection drops for 1 minute, the driver will attempt to re-connect and will leave the client's request hanging till the reconnection is successful. So, things like socketTimeoutMS (which I managed to get to work) are not effective here.
What's the best way to get MongoDB to "give up" on a request after N seconds?
The last thing I want is give the client a timeout error -- with the server actually completing the requests 5 minutes later!
There is maxTimeMS option introduced in 2.6:
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:27017/test", function(err, db) {
// Get an aggregation cursor
var cursor = db.collection('data')
.find("$where": "sleep(1000) || true")
.maxTimeMS(50);
// Get alll the items
cursor.toArray(function(err, items) {
console.dir(err);
console.dir(items);
db.close();
});
});