45

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 varies. Something like this:

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

What would be the correct syntax for this?

Maciej Krawczyk
  • 14,825
  • 5
  • 55
  • 67

4 Answers4

101

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/

Kevin
  • 24,871
  • 19
  • 102
  • 158
21

You can also use.

Site.remove({ userUID: uid, id: { $in: [10, 2, 3, 5]}}, function(err, response) {});
laxman
  • 1,338
  • 2
  • 10
  • 27
11

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

estani
  • 24,254
  • 2
  • 93
  • 76
7

Yes, $in is a perfect solution :

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

Amr Saber
  • 988
  • 10
  • 26
Ramana V V K
  • 1,245
  • 15
  • 24