forEach doesn't iterate all collection in mongodb

旧时模样 提交于 2019-12-07 23:03:16

问题


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]));
})

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);

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.

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.

Why foreach could iterate not all items and how to fix it ?

[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.

[UPDATE 2] I tried to run script using native mongo shell mongo.exe and got 100 from 367 elements printed, without errors


回答1:


[Updated]

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]
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. See http://blog.robomongo.org/robomongo-is-robo-3t/#4a. So no need to change config file as explained below for older versions.

Workaround solution for earlier versions than 1.1:

Make sure Robomongo is closed. Open robomongo.json configuration file.

[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
  1. Change "shellTimeoutSec" attribute value to higher number in seconds. (i.e. "shellTimeoutSec" : 60)
  2. Save config file and re-launch Robomongo app.
  3. Run necessary script. If script isn't executed entirely, try to increase shellTimeoutSec value.

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



来源:https://stackoverflow.com/questions/39838221/foreach-doesnt-iterate-all-collection-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!