Pregunta

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
        });
¿Fue útil?

Solución

Previous answers helped me but the exact answer I was looking for was passing more arguments in update callback like below:

myCollection.update(..., {upsert : true}, function(err, numberAffected, raw) {
    console.log(raw);

}):

if the update was an insert you get item's _id from raw.upserted updatedExisting: false, upserted: 52f6b9756268a019bd11d0fb,

Ref:

https://groups.google.com/d/msg/mongoose-orm/ehZ11QY-OUw/-ex7ekL2c9sJ https://github.com/mafintosh/mongojs/issues/39

Otros consejos

GetLastError following the upsert operation should provide you details on whether it was inserted or updated. If the driver / framework you are using does not expose these values, I would suggest raising request with maintainers.

The structure you are looking for is getLastError, where an insert occured an object Id will be returned in one of the fields. All are explained depending on the operation you are doing.

For the node native driver the documentation for the function is here. Most of the drivers are maintained by MongoDB staff so they are sure to be compliant with the standards.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top