Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "deletemany" and "remove" in mongodb?

Tags:

mongodb

What's the difference between the two commands here?
db.collection.deleteMany({condition})
db.collection.remove({condition})

like image 651
look Avatar asked Feb 29 '16 03:02

look


People also ask

What is difference between deleteMany and remove in MongoDB?

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.

What is deleteMany in MongoDB?

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.

What is remove in MongoDB?

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.

What does deleteMany return in mongoose?

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.


Video Answer


1 Answers

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 } 
like image 110
Revol89 Avatar answered Oct 07 '22 19:10

Revol89