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