MongoDB

How to use deleteMany method in MongoDB

MongoDB lies under the umbrella of the NoSQL category of datbases that has an extensive catalog of methods. These methods include collection methods, cursor methods, database methods, and many more like these. The collection methods are the most used among all and the deleteMany() method is a part of collection methods.

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.

In this article, we will provide a detailed demonstration to apply the deleteMany() method to a MongoDB collection.

Let’s start this tutorial by understanding the working of this method:

How deleteMany() method works in MongoDB

Like other methods do follow a specific syntax: similarly, to use deleteMany() method; the following syntax must be followed:

db.collection-name.deleteMany({<document1>}, {<document2>}....)

In the syntax, the “collection-name” refers to the collection on which the delete method will be applied.

As discussed earlier, deleteMany() method belongs to the collection methods of the MongoDB database; so to apply this method, you should have the following MongoDB based prerequisites on your system:

Database: A valid MongoDB database is required, and we will be using “linuxhint” as a database name in this tutorial.

Collection: For better understanding, we have used multiple collections in this guide. Each example is provided with a different database.

After the creation of the database and the collection; you must have some documents inside that collection to apply the deleteMany() method.

How to use deleteMany() method in MongoDB

This section comprises several examples that explain the implementation of the deleteMany() method in multiple possible scenarios.

Example 1: Using deleteMany() method to delete all documents

In this example, the “inventory” database of “linuxhint” database is used, and we have executed the following MongoDB command to get all the documents present in the collection.

> db.inventory.find().pretty()

Text Description automatically generated

The deleteMany() method is practiced here to delete all the documents present in the inventory collection. For this, the deleteMany() method must be executed without any parameters or documents.

To do so, the below-mentioned command will enable you to delete all documents:

> db.inventory.deleteMany({})

Logo Description automatically generated

Example 2: Using deleteMany() method to delete specific document(s)

Most of the developers intend to apply the deleteMany() method on the documents that meet the conditions specified by them. Before proceeding, let’s have a look at the content of “laptops” collection by using find() method:

> db.laptops.find().pretty()

For instance, we want to delete the documents that are no longer available, and the “status” field shows the availability of any document. In the below mentioned MongoDB query, the deleteMany() method will remove all the documents in which the “status” field matches the “sold” keyword:

Note: The “deletedCount” number is equal to “1”, which means this command deletes the “1” document.

> db.laptops.deleteMany({status: "sold"})

Graphical user interface Description automatically generated with medium confidence

Example 3: Using deleteMany() method with conditional operators

It is noticed that the deleteMany() method can be used with conditional operators to delete only those documents that meet the condition.

In the following command, only those documents are deleted that have “price” value greater than “1000“:

> db.laptops.deleteMany({price: {$gt: 1000}})

Graphical user interface Description automatically generated with low confidence

Similarly, other conditional operators like $lt, $lte, $gte can also be tried with deleteMany() method.

Note: The collection (“laptops“) used here is the same as of Example 2.

Example 4: Using deleteMany() method with logical operators

The logical operators supported by MongoDB include $and, $or, $nor, $not, and these all operators can be practiced with the deleteMany() method to delete a specific set of documents.

For instance, the below mentioned command will use “$and” operator to delete all documents that satisfies the following condition where “make” value matches ” HP” and the “category” field equals to “gaming” value :

> db.laptops.deleteMany({$and: [{make: "HP"}, {category: "gaming"}]})

A picture containing website Description automatically generated

Example 5: Use deleteMany() with logical and conditional operators

Getting into more depth, the insertMany() method can also be used with logical and conditional operators at a time. The command given below will delete all those documents that satisfies the following condition:

Condition for deletion: Either the “price” value is less than “500” or the “category” matches the value “kids“:

> db.laptops.deleteMany({$or: [{price: {$lt: 500}},{category: "kids"}]})

Note: The “laptops” collection from the “linuxhint” database is used for this example.

Conclusion

The MongoDB collection methods are the core part of dealing with Mongo-based databases. As MongoDB stores data in document form in a collection and several collection methods are used to perform CRUD operations that include creation, retrieval, updating, and deletion of documents. Our today’s guide is focused on providing an insight into the “deleteMany()” method of MongoDB. The MongoDB enthusiasts can follow this guide to delete all documents from a MongoDB collection at once. However, one can delete a few selected documents as well, by specifying the condition.

About the author

Adnan Shabbir