var fromColl = db.from.find(),
fromDoc,
toColl,
toDoc;
//iterate through original collection
fromColl.forEach(function(fromObj){
//find by name in target collection
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
toColl.forEach(function(toObj){ // <- not executed
if (equal(fromObj.data, toObj.data)) {
//duplicate, skip
} else {
//...
}
});
}
});Even if toColl has elements, the second forEach loop isn't executed. What am I doing wrong?
toColl = db.to.find({name: fromObj.name});var toColl = db.to.find({name: fromObj.name});Actually toColl is already declared at the top, and it doesn't seem to make a difference if I declare it inside the first forEach function.ÂBoth ways toColl.length() will return the correct value, so the conditional part works as expected.I found a workaround, and created an array of the second cursor:Â toColl = db.to.find({name: fromObj.name}).toArray();Â and I iterated the array with a plain JS for loop.So my problem is solved, but it would be good to know why the nested cusor.forEach() is not executed...
 if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents in the results.
var fromColl = db.from.find();
fromColl.forEach(function(fromObj){
 var toColl = db.to.find({name: fromObj.name});
 if (toColl.length() == 0) {
  db.to.insert(fromObj);
 } else {
  print(toColl.length());
  toColl.forEach(function(toObj){ // <- not executed
   print('inside forEach');
  });
 }
});
if (!toColl.length())if (!toColl)print('duplicate? - found with same name: ' + toColl.length());print('duplicate? - found with same name: ' + toColl);total: 1 - inserted: 0 - skipped: 0
duplicate? - found with same name: DBQuery: test.toColl -> { "name" : "aaa" }
inside forEach - toObj.name: aaa
2017-06-13T16:53:50.594+0200 E QUERY Â Â [thread1] TypeError: arr1 is undefined :
equal@(shell):2:17
@(shell):16:5
DBQuery.prototype.forEach@src/mongo/shell/query.js:488:1
@(shell):14:1
DBQuery.prototype.forEach@src/mongo/shell/query.js:488:1
@(shell):1:1if (equal(fromObj.nutr, toObj.nutr))if (equal(fromObj, toObj))mongos> db.toColl.find({});
{ "_id" : ObjectId("593fe737e0382abce8cb751e"), "name" : "aaa", "data" : [ { "id" : "a", "val" : 1 }, { "id" : "b", "val" : 2 } ] }
{ "_id" : ObjectId("593fe737e0382abce8cb7521"), "name" : "bbb", "data" : [ { "id" : "a", "val" : 1 }, { "id" : "b", "val" : 2 } ] }
{ "_id" : ObjectId("593fe737e0382abce8cb7524"), "name" : "ccc", "data" : [ { "id" : "a", "val" : 1 }, { "id" : "b", "val" : 2 } ] }mongos> var c = db.fromColl.find()
mongos> c
{ "_id" : ObjectId("593fe737e0382abce8cb751e"), "name" : "aaa", "data" : [ { "id" : "a", "val" : 1 }, { "id" : "b", "val" : 2 } ] }
{ "_id" : ObjectId("593fe737e0382abce8cb751f"), "name" : "aaa", "data" : [ { "id" : "a", "val" : 1 }, { "id" : "b", "val" : 2 } ] }
{ "_id" : ObjectId("593fe737e0382abce8cb7520"), "name" : "aaa", "data" : [ { "id" : "a", "val" : 2 }, { "id" : "b", "val" : 4 } ] }
{ "_id" : ObjectId("593fe737e0382abce8cb7521"), "name" : "bbb", "data" : [ { "id" : "a", "val" : 1 }, { "id" : "b", "val" : 2 } ] }
{ "_id" : ObjectId("593fe737e0382abce8cb7522"), "name" : "bbb", "data" : [ { "id" : "a", "val" : 2 }, { "id" : "b", "val" : 4 } ] }
{ "_id" : ObjectId("593fe737e0382abce8cb7523"), "name" : "bbb", "data" : [ { "id" : "a", "val" : 3 }, { "id" : "b", "val" : 6 } ] }
{ "_id" : ObjectId("593fe737e0382abce8cb7524"), "name" : "ccc", "data" : [ { "id" : "a", "val" : 1 }, { "id" : "b", "val" : 2 } ] }
{ "_id" : ObjectId("593fe737e0382abce8cb7525"), "name" : "ccc", "data" : [ { "id" : "a", "val" : 1 }, { "id" : "b", "val" : 2 } ] }
{ "_id" : ObjectId("593fe737e0382abce8cb7526"), "name" : "ccc", "data" : [ { "id" : "a", "val" : 2 }, { "id" : "b", "val" : 4 } ] }
mongos> c
mongos> var c = db.fromColl.find()
mongos> c.length()
9
mongos> c
cursor.length()cursor.count()if (equal(fromObj.data, toObj.data))