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?
You can delete multiple documents in a collection at once using the collection. deleteMany() method. Pass a query document to the deleteMany() method to specify a subset of documents in the collection to delete.
To delete all documents in a collection, pass an empty document ( {} ). Optional. To limit the deletion to just one document, set to true . Omit to use the default value of false and delete all documents matching the deletion criteria.
drop() will delete to whole collection (very fast) and all indexes on the collection.
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;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With