问题描述
我需要比较来自mongo db的两个对象集合。
我的shell脚本如下所示:
I need to compare two collections of objects from mongo db.My shell script looks like this:
//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]));
})
我在Robomongo中执行此脚本。
但我有一个问题。结果只打印了367项中的8项。 Robomongo没有显示任何错误消息。
当我使用
I execute this script in the Robomongo.But i have a problem. At the result only 8 items from 367 was printed. Robomongo didn't show any error messages.When i used
print(item);
在foreach内部,一切正常,所有367个对象都已打印出来。此外,我尝试使用Deep Diff库进行对象比较但结果相同 - 只打印了367个项目。
inside foreach, all worked fine and all 367 objects was printed. Also i tried to use Deep Diff library for objects comparison but got same result - only 12 items from 367 was printed.
我认为内存消耗问题,但我不知道如何处理它,以及为什么Robomongo不会打印任何错误。
I think the problem in the memory consumption, but i don't know how to handle it, and why Robomongo doesn't print any errors.
我试图只迭代游标,但它没有帮助。
I tried to iterate only cursors but it didn't help.
为什么foreach可以迭代所有项目以及如何修复它?
Why foreach could iterate not all items and how to fix it ?
[UPDATE 1]
经过一段时间的调查后,我提到如果我在Robomongo刚刚打开的标签中运行脚本,它会输出102个元素,但是当我在同一个标签中再次运行它时,它只打印12个。
[UPDATE 1]After some time of the investigation i mentioned that if i run script in just opened tab in Robomongo it prints 102 elements, but when i run it again in the same tab it prints only 12.
[更新2]
我尝试使用原生mongo shell mongo.exe运行脚本,并打印出367个元素中的100个,没有错误
[UPDATE 2]I tried to run script using native mongo shell mongo.exe and got 100 from 367 elements printed, without errors
推荐答案
[更新]
可能与此有关超时Robomongo默认超时为15秒,位于配置文件中,可以更改。这可能会有所帮助:
It might be related to the timeout. Robomongo has 15 seconds default timeout which is in the config file and it can be changed. This might help:
[Update-1]
从版本Robo 3T - 1.1(以前称为Robomongo)开始, shell超时可在UI上配置,并且还有一个修复程序可以防止此问题发生。请参阅
。因此,无需更改配置文件,如下面针对旧版本所述。
[Update-1]
Starting from version Robo 3T - 1.1 (formerly Robomongo), the shell timeout is configurable on UI and also there is a fix to prevent this issue happening. Seehttp://blog.robomongo.org/robomongo-is-robo-3t/#4a. So no need to change config file as explained below for older versions.
早于1.1版的解决方案解决方案:
[更新]:添加指向适用于较新版本。
[Update]: Adding link to Robomongo-Config-File-Guide for newer versions.
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
- 将shellTimeoutSec属性值更改为更高的数字(以秒为单位)。 (即shellTimeoutSec:60)
- 保存配置文件并重新启动Robomongo应用程序。
- 运行必要的脚本。如果脚本未完全执行,请尝试增加shellTimeoutSec值。
参考:
这篇关于forEach不会迭代mongodb中的所有集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!