Remove multiple documents from mongo in a single query

后端 未结 5 876
栀梦
栀梦 2020-11-30 02:16

I have a list of mongo \'_id\' which I want to delete. Currently I am doing this

# inactive_users -->  list of inactive users 
for item in inactive_users:         


        
5条回答
  •  清歌不尽
    2020-11-30 02:55

    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);
    });
    

提交回复
热议问题