I try to fetch feeds from multiple endpoints periodically and looping these batch of items and do update with upsert:true.
For each item I check the URL of an item
if it's already in myCollection, it updates the "end_date"
if it's new, it gets inserted in myCollection with "start_date" using $setOnInsert
My question is: How can I get only inserted items upon an upsert from myCollection?
I tried findAll $where start_date = end_date but $setOnInsert: { start_date: end_date }
didn't make both dates exactly equal. And this solution is not efficient even if it worked as I perceived from earlier questions. Looking for the best practices. Thanks!
Here is my code:
var now = new Date(); ///???????? I thought this was cached??
for (var i = batch.length - 1; i >= 0; i--) {
batch[i].end_date = now;
myCollection.update({'url': batch[i].url}, {$setOnInsert: { start_date: batch[i].end_date }, $set: batch[i]}, {safe:true, upsert : true}, function(err, result) {
if(err) { console.log(err); return; }
console.log(result); //result doesn't give any clue about inserts
});