If there are two nested cursor.forEach()
functions, the second one is not getting executed. The same happens with a while
loop:
I want to remove duplicates from a huge collection, by moving documents to another collection, and checking if a duplicate already exists. I'm running the following code in the mongo shell:
var fromColl = db.from.find(),
toColl;
fromColl.forEach(function(fromObj){
toColl = db.to.find({name: fromObj.name});
if (toColl.length() == 0) {
//no duplicates found in the target coll, insert
db.to.insert(fromObj);
} else {
//possible duplicates found in the target coll
print('possible duplicates: ' + toColl.length());
toColl.forEach(function(toObj){
if (equal(fromObj.data, toObj.data)) {
//duplicate...
}
});
}
});
In the else block toColl.length()
is printed, but the second forEach loop isn't executed. Does anyone know why?