I am copying 157 documents from 1 collection to another. I just want to count how many documents are inserted or skipped but it always gives wrong output (like only 100 but it should be 157 as the destination column was empty and all of 157 are inserted now). Any help will be heartily appreciated.
var cursor = await collection.find(query).project(projection);
var countInserted = 0;
var countSkipped = 0;
await cursor.forEach(async function(i) {
i.ts_imported = new Date();
//console.log(i);
ret = await EmptyCollection.updateOne(
{ URL: i.sourceURL },
{
$setOnInsert: {
URL: i.sourceURL,
status: 0,
remarks: "in the run got 0 jobs",
collection: col
}
},
{ upsert: true }
);
if (await ret.upsertedCount == 0) {
++countSkipped;
}
else {
++countInserted;
}
//console.log(countInserted); //This shows actual count.
});
console.log(countInserted); //This shows wrong count.
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(countInserted+' rows inserted.<br/>'+countSkipped+" rows skipped.");
res.end();