在MongoDB中处理(长期)连接丢失

我正在写一个使用商店的Web应用程序。 如果客户端在30秒内没有收到响应,则认为请求已经失效,并且发生超时错误。

我试图让MongoDB做同样的事情。 例如,如果连接下降1分钟,则驱动程序将尝试重新连接并挂起客户端的请求,直到重新连接成功。 所以,像socketTimeoutMS (我设法工作)的东西在这里没有效果。

什么是最好的方式让MongoDB在N秒后对请求“放弃”?

我想要的最后一件事情是给客户端一个超时错误 – 服务器在5分钟后实际完成请求!

2.6中引入了maxTimeMS选项:

 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(); }); });