Update multiple documents by providing documents in body, mongoose/mongodb

前端 未结 1 1949
小蘑菇
小蘑菇 2021-02-08 08:09

I need to update several documents by providing them in the body. I can\'t query them, they must be provided.

Example:

 var persons = [
    {id: 1, name         


        
相关标签:
1条回答
  • 2021-02-08 08:30

    Try using the update command along with the "$in" operator:

    var ids= [];
    for (var i=0 i<input.body.length; ++i) {
        ids.push(input.body[i].id);
    }
    
    mongoose.model('person').update( {id : {"$in":ids}}, {active:false} , {multi: true} , function(err,docs) { ... });
    

    Hope this helps

    0 讨论(0)
提交回复
热议问题