MongoDB: deletemany

deleteMany()

Syntax

deleteMany(Filter,Options)

deleteMany() command in MongoDB is used to delete multiple documents according to the parameters passed. Like deleteOne() this command also follows two same parameters filter and options as discussed earlier.

filter parameter sets the criteria for the deletion of documents. While options parameter includes writeConcern, Collation, hint, etc. which we are going to discuss in the later part of this course series. Now, let's discuss the functioning of deleteMany() by below given example:

Example

For Instance, we have 4 documents present inside a collection:

MONGODB

From the above 4 documents, 2 contains Department as Sales. Now, You want to delete all those documents that contain Department as Sales. Follow the code below for the same:

db.edatabase.deleteMany({Department: 'Sales'})

Output

MONGODB