Reputation: 1982
I need to remove a set of ids using an object which contains those ids. Right now I am iteratively removing ti. Is it possible to remove them at once?
My code (iteratively):
remove_data(doc[0]._id,function(result){
for(entry in result) {
db.mydata.remove({"_id":result[entry]}, function(err,doc) {
if(err) throw err;
});
}
});
Also I need to pass a callback after this is done.
Upvotes: 0
Views: 56
Reputation: 56467
First combine IDs into a list:
var ids = [];
for (var k in result) {
if (result.hasOwnProperty(k)) {
ids.push(result[k]);
}
};
and then
db.mydata.remove({"_id": {"$in": ids}}, function(err) {
// ...
});
Side note: if you are dealing with multiple asynchronous tasks and you have to call a callback after all of them finish then you should use some kind of asynchronous control flow library. Caolan's async.js is a good choice:
https://github.com/caolan/async
Upvotes: 3