Mongoose - remove multiple documents in one function call

Maciej Krawczyk picture Maciej Krawczyk · Jun 10, 2017 · Viewed 43.8k times · Source

In documentation there's deleteMany() method

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

I want to remove multiple documents that have one common property and the other property vary. Something like this:

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

What would be the proper syntax for this?

Answer

Kevin picture Kevin · Jun 10, 2017

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/