2016-09-27 54 views
2

在RoboMongo(0.9.0-RC09)中运行以下mongo查询会提供正确数量的文档(使用游标计数函数),而迭代所有文档只返回一小部分文档:MongoDB游标不会返回所有文档

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 

我们是否需要专门配置查询返回所有文件?

回答

4

问题解决了: 这是robomongo shellTimeoutSec结构的一个问题:这引起了光标停止返回多个元素(默认15秒)。

这也解释了“随机”数量从30 000到44 000(取决于网络速度)。 这里是robomogo的票:https://github.com/paralect/robomongo/issues/1106#issuecomment-230258348

的修复/解决办法,现在是增加robomongo.json shellTimeoutSec

Windows 
0.9.x 
    C:\Users\<user>\.config\robomongo\0.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 
+0

ohhh,我发现它,thx上帝。 –

+0

谢谢!当我意识到并非所有的更新都已完成,但没有诊断/警告 - 跆拳道? – randomsock

-1

我们需要转换成数组。之后,只有我们可以为每个人做。 试试看!!!

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);}); 
+0

的forEach定义光标,并应工作,因为文件中的https:/ /docs.mongodb.com/manual/reference/method/cursor.forEach/(我们使用mongodb 3.2.6) – hupfis

+0

这个建议会加载内存中的所有数据。不一定是个好主意。 – qqilihq

+0

嗨Hupfis!一旦操作完成,光标将立即关闭。它会在光标关闭一段时间后生效,所以你不能得到结果。我将编辑你试试这个的答案 –