在加盖的集合上放慢可拖动的光标

我使用MubSub来允许用户订阅特定的查询,并在可用时立即获取推送的更新。 该库使用加盖的集合来获得可放大的游标。 我遇到的问题是,当我只有一个可放大的光标时,一切都很顺利。 大约需要几ms来获取游标。 但是,当我添加更多的订阅(从而打开更多的游标),光标的接收有时可能需要8秒钟。 我试过添加索引,但是根本没有帮助。

这里是我的collections的统计数据:

{ "ns" : "mDB.myCollection", "count" : 395669, "size" : 325551880, "avgObjSize" : 822.7884418541761, "storageSize" : 1000001536, "numExtents" : 1, "nindexes" : 3, "lastExtentSize" : 1000001536, "paddingFactor" : 1, "flags" : 1, "totalIndexSize" : 81678240, "indexSizes" : { "subscriptionIndex" : 32704000, "_id_" : 11593568, "subscriptionQueryAsc" : 37380672 }, "capped" : 1, "max" : 2147483647, "ok" : 1 } 

这是一段执行时间过长的代码:

 this.collection.then(handle(true, function(collection) { var latest = null; // The next statement takes a few ms for the first cursor, // then 5+ seconds for more cursors collection.find({}).sort({ $natural: -1 }).limit(1).nextObject(handle(function(doc) { if (doc) latest = doc._id; (function poll() { if (latest) query._id = { $gt: latest }; var options = { tailable: true, awaitdata: true, numberOfRetries: -1 }; var cursor = collection.find(query, options).sort({ $natural: 1 }); (function more() { cursor.nextObject(handle(function(doc) { if (!doc) return setTimeout(poll, self.wait); callback(doc); latest = doc._id; more(); })); })(); })(); })); })); 

这是一个已知的问题,或者我只是做错了什么?

我通过删除上面粘贴的代码中的以下行来解决此问题:

 collection.find({}).sort({ $natural: -1 }).limit(1).nextObject(handle(function(doc) { 

该特定的声明使得代码非常慢,可能是因为它正在提取所有({})文档,并且不知怎的,游标的数量减慢了进程的速度。 我做了这样的事情:

 this.collection.then(handle(true, function(collection) { var latest = null; if (doc) latest = doc._id; (function poll() { if (latest) query._id = { $gt: latest }; var options = { tailable: true, awaitdata: true, numberOfRetries: -1 }; var cursor = collection.find(query, options).sort({ $natural: 1 }); (function more() { cursor.nextObject(handle(function(doc) { if (!doc) return setTimeout(poll, self.wait); callback(doc); latest = doc._id; more(); })); })(); })(); })); 

我不完全理解为什么MubSub的作者这样做,因为它不关心最后一个文档的_id是什么。 这是因为文档被插入到一个加盖的集合中,这会保留插入顺序。