What's the difference between the two commands here?db.collection.deleteMany({condition})
db.collection.remove({condition})
deleteMany: command returns a boolean as true if the operation runs fine with the write concern and returns false if we disable the write concern. Also, it returns the deletedCount which contains the deleted document's number. Remove: command returns the WriteResult.
The deleteMany() method allows you to remove multiple documents from a specific collection of MongoDB databases. It can be used to remove all documents as well or one can specify the condition to delete documents using the deleteMany() method.
MongoDB's remove() method is used to remove a document from the collection. remove() method accepts two parameters. One is deletion criteria and second is justOne flag. deletion criteria − (Optional) deletion criteria according to documents will be removed.
The deleteMany() method returns an object that contains three fields: n – number of matched documents. ok – 1 if the operation was successful. deletedCount – number of deleted documents.
They do the same. The difference is the values that return.
With remove()
:
> db.ticker.remove({"name": "Bitcoin"}) WriteResult({ "nRemoved" : 2 })
With deleteMany()
:
> db.ticker.deleteMany({"name": "Bitcoin"}) { "acknowledged" : true, "deletedCount" : 2 }
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