10

I have an array of _ids, like, for example

["a12s", "33qq", "121a"]

I know, there are two method in MongoDB like deleteMany, where I can delete by specific query

  var myquery = { address: 'abc' };
  dbo.collection("customers").deleteMany(myquery, function(err, obj) {
    if (err) throw err;
  });

and deleteOne, where I could delete one specific chosen document.

I would like to delete the documents with the ids from a given array, but can't find anywhere in documentation something about this. Is it possible in MongoDB?

Anna F
  • 1,583
  • 4
  • 22
  • 42
  • you can use `$in` like this `db.collection.deleteMany({'_id': {'$in':list_of_ids}})` https://stackoverflow.com/questions/18566590/remove-multiple-documents-from-mongo-in-a-single-query – Get Off My Lawn May 30 '18 at 20:35

1 Answers1

13

You are looking for the $in operator. It matches values in an array. You can use it in the deleteMany method filter parameter.

var ids = ["a12s", "33qq", "121a"];
var myquery = { _id: { $in: ids } };
  dbo.collection("customers").deleteMany(myquery, function(err, obj) {
    if (err) throw err;
  });
Boric
  • 822
  • 7
  • 26