Mongoose - remove multiple documents in one function call

后端 未结 4 726
鱼传尺愫
鱼传尺愫 2021-01-03 17:55

In documentation there\'s deleteMany() method

Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }, function (err) {});

I want to remo

相关标签:
4条回答
  • 2021-01-03 18:28

    You can also use.

    Site.remove({ userUID: uid, id: { $in: [10, 2, 3, 5]}}, function(err, response) {});
    
    0 讨论(0)
  • 2021-01-03 18:28

    Yes, $in is a perfect solution :

    Site.deleteMany({ userUID: uid, id: { $in: [10, 2, 3, 5] } }, function(err) {})

    0 讨论(0)
  • 2021-01-03 18:38

    I believe what youre looking for is the $in operator:

    Site.deleteMany({ userUID: uid, id: { $in: [10, 2, 3, 5]}}, function(err) {})
    

    Documentation here: https://docs.mongodb.com/manual/reference/operator/query/in/

    0 讨论(0)
  • 2021-01-03 18:38

    I had to change id to _id for it to work:

    Site.deleteMany({ _id: [1, 2, 3] });
    

    This happens if no id is defined and the default one is used instead:

    "Mongoose assigns each of your schemas an _id field by default if one is not passed into the Schema constructor." mongoose docs

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