Delete Multiple Documents Using MongoDB Shell Examples

Let’s look at some examples of how to delete multiple documents using MongoDB Shell.

In the following examples, we are working with:

Database: w3wiki
Collection: contributor
Document: four documents that contain the details of the contributors in the form of field-value pairs.

Delete Documents that Match a Filter Example

In this example, we are deleting multiple documents from the contributor collection that match the filter, i.e., language: “C#”.

Deleting All Documents Example

In this example, we are deleting all the documents from the contributor collection by passing empty documents in the db.collection.deleteMany() method.

db.contributor.deleteMany({})

MongoDB – Delete Multiple Documents Using MongoDB Shell

The db.collection.deleteMany() method is used to delete multiple documents from a collection in Mongo Shell. This method deletes multiple documents from the collection according to the filter.

The deleteMany() is a Mongo shell method, which can delete multiple documents. This method can be used in multi-document transactions. If you use this method in a capped collection, then it will throw an exception.

Similar Reads

Syntax

db.collection.deleteMany(    ,   {      writeConcern: ,     collation:    })...

Delete Multiple Documents Using MongoDB Shell Examples

Let’s look at some examples of how to delete multiple documents using MongoDB Shell....

Conclusion

In this article, we have explained how to delete multiple documents using Mongo Shell. To delete multiple documents from a collection, we can use the db.collection.deleteMany() method....

Contact Us