I need to update some documents in one collection and send an array of _idupdated documents to another collection.
Since it update()returns the number of updated elements, and not their identifiers, to get an array, I came up with the following:
var docsUpdated = [];
var cursor = myCollection.find(<myQuery>);
cursor.forEach(function(doc) {
myCollection.update({_id : doc._id}, <myUpdate>, function(error, response){
docsUpdated.push(doc._id);
});
});
Or I could do:
var docsUpdated = myCollection.distinct("_id", <myQuery>);
myCollection.update(<myQuery>, <myUpdate>, {multi : true});
I assume the second version will be faster because it calls the database twice. But both seem annoyingly ineffective - is there any other way to do this without multiple database calls? Or am I embarrassing something?
source
share