MongoDB游标不return所有文件

MongoDB cursor does not return all documents

运行 RoboMongo (0.9.0-RC09) 中的以下 mongo 查询给出了正确数量的文档(使用游标计数函数),而迭代所有文档仅 return 一小部分文件:

var allDocuments = db.getCollection('mycollection').find({});
print(allDocuments.size());  // prints 170 000 -> correct

var count = 0;
allDocuments.forEach(function(doc) {
    count++;
});
print(count); // 'randomly' prints values between 30 000 and 44 000

我们是否需要将查询专门配置为 return 所有 个文档?

我们需要转换成数组。在那之后只有我们可以做 forEach。 试试下面!!!

var allDocuments = db.getCollection('mycollection').find({}).toArray();
print(allDocuments.length);  
var count = 0;
allDocuments.forEach(function(doc) {
count++;
print("IterCount : ",count); 
});
print("FinalCount : ",count); 

// 带光标

db.getCollection('mycollection').find({}).forEach(function(doc){
count++;
print("IterCount : ",count);});

问题已解决: 这是 robomongo shellTimeoutSec 配置(默认值:15 秒)的问题,导致光标停止返回更多元素。

这也解释了 'random' 30 000 到 44 000 的计数(取决于网络速度)。 这是 robomogo 的门票:https://github.com/paralect/robomongo/issues/1106#issuecomment-230258348

目前的fix/workaround是在robomongo.json中增加shellTimeoutSec

Windows
 0.9.x
  C:\Users\<user>\.config\robomongo[=10=].9\robomongo.json
 0.8.x
  C:\Users\<user>\.config\robomongo\robomongo.json   
MAC
 0.9.x
  /Users/<user>/.config/robomongo/0.9/robomongo.json
 0.8.x
  /Users/<user>/.config/robomongo/robomongo.json     
Linux
 0.9.x
  /home/<user>/.config/robomongo/0.9/robomongo.json
 0.8.x
  /home/<user>/.config/robomongo/robomongo.json