Remove multiple documents from mongo in a single query
You need to pass the ids in a specific format using ObjectId()
:
db.users.remove({_id: {$in: [ObjectId('Item1'), ObjectId('Item2'), ObjectId('Item2')]}});
Remove
doesn't accept integer - you have to use ObjectId
instance with _id
format as a string
.
db.users.deleteMany({'_id':{'$in':inactive_users}})
List them all and use $in
operator:
db.users.remove({_id:{$in:[id1, id2, id3, ... ]}})
var collection = db.users;
var usersDelete = [];
var ObjectID = req.mongo.ObjectID; //req is request from express
req.body.forEach(function(item){ //req.body => [{'_id' : ".." , "name" : "john"}]
usersDelete.push(new ObjectID(item._id));
});
collection.remove({'_id':{'$in': usersDelete}},function(){
//res.json(contatos);
});