forEach 不会迭代 mongodb 中的所有集合

Eug*_*ene 3 javascript mongodb robo3t

我需要比较 mongo db 中的两个对象集合。我的 shell 脚本如下所示:

//Both arrays have 367 pretty big objects.
var list1 = db.collection1.find({..condition..}).toArray(); 
var list2 = db.collection2.find({..condition..}).toArray();

function compare(left, right){
   var l = left.data.NP;
   var r = right.data.NP;
   if(JSON.stringify(l) === JSON.stringify(r)){
      return 'Equal';
   } else {
      return 'Not equal';
   }
}

list1.forEach(function(item, index){
   print(index, compare(item,list2[index]));
})
Run Code Online (Sandbox Code Playgroud)

我在 Robomongo 中执行这个脚本。但我有一个问题。结果,367 项中仅打印了 8 项。Robomongo 没有显示任何错误消息。当我使用

print(item);
Run Code Online (Sandbox Code Playgroud)

在 foreach 内部,一切正常,所有 367 个对象都被打印出来。我还尝试使用 Deep Diff 库进行对象比较,但得到了相同的结果 - 仅打印了 367 个项目中的 12 个项目。

我认为问题出在内存消耗上,但我不知道如何处理它,也不知道为什么 Robomongo 不打印任何错误。

我尝试仅迭代游标,但没有帮助。

为什么 foreach 不能迭代所有项目以及如何修复它?

[更新 1] 经过一段时间的调查,我提到,如果我在 Robomongo 中刚刚打开的选项卡中运行脚本,它会打印 102 个元素,但是当我在同一个选项卡中再次运行它时,它只会打印 12 个元素。

[更新 2] 我尝试使用本机 mongo shell mongo.exe 运行脚本,并从 367 个元素中打印出 100 个,没有错误

Gok*_*sek 5

[更新]

可能和超时有关。Robomongo 有 15 秒的默认超时,该超时位于配置文件中并且可以更改。这可能有帮助:

[Update-1]
从版本 Robo 3T - 1.1(以前称为 Robomongo)开始,shell 超时可在 UI 上配置,并且还修复了防止此问题发生的问题。请参阅 http://blog.robomongo.org/robomongo-is-robo-3t/#4a。因此,无需更改旧版本的配置文件,如下所述。

1.1之前版本的解决方案:

确保 Robomongo 已关闭。打开 robomongo.json 配置文件。

[更新]:添加新版本Robomongo-Config-File-Guide的链接。

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
Run Code Online (Sandbox Code Playgroud)
  1. 将“shellTimeoutSec”属性值更改为更高的数字(以秒为单位)。(即“shellTimeoutSec”:60)
  2. 保存配置文件并重新启动 Robomongo 应用程序。
  3. 运行必要的脚本。如果脚本没有完全执行,请尝试增加 shellTimeoutSec 值。

参考: https ://github.com/paralect/robomongo/issues/1106#issuecomment-230258348