Deleting Documents in MongoDB

MongoDB

While working with MongoDB, you might feel the need to delete certain or multiple documents from your collection. This is why there are multiple parameters and methods at use, that you can apply to fit your criteria while doing your task. Want to delete files only related to certain criteria? It’s possible. Here, below you’ll find the instructions to Mongo db delete documents according to your specific requirements.

According to Function

●   When Deleting All Documents Matching a Certain Criteria/Condition

Suppose you wish to delete documents from a collection that only fit certain criteria. You can use specified filters to specify your condition for the deletion of documents. For this, you’ll need to use the method: db.(collection name).deleteMany({Field:”condition value”}). Let’s say for instance, you wish to filter out documents that include client details who are below the age of 21, from the collection customers, you’ll need to write:

db.customers.deleteMany({age:{$lt:21}})

This’ll delete all the files which include details of clients who are younger than 21. Let’s use another example;

db.customers.deleteMany({title:”2020”})

That’ll help you delete all documents that have the numbers 2020 in their titles.

●  When Deleting One Document that Meets a Condition

In this case, you’ll need to use the method – db.(collection name).deleteOne(field:condition value). For instance, you need to delete a file of a particular client named David Beverly. Here you’ll need to write:

db.customers.deleteOne({name:”David Beverly”});

And your work is done.

●  When Deleting a Document Without Condition

In case you were to pass an empty filter to the method deleteOne(), what would be the result? Let’s say you use the same command as specified in the previous one, but simply pass an empty document filter:

db.customers.deleteOne({})

This would delete only the first document from the collection customers.

●  When Deleting All Documents

If you wish to delete every document stored in a collection, you need to pass an empty document filter to the method – db.(collection name).deleteMany(). Instead of passing specified document criteria, passing an empty document filter allows you to delete all the documents from the specified collection. For instance, you wish to delete documents from the collection customers, you’ll write it as:

db.customers.deleteMany({})

The use of the query operators, (the filter) will provide you the result of the status of the operation.

Alternate Methods to Use for Deleting Documents

When working with MongoDB, the above-specified methods aren’t the only ones you can use but are usually the preferred ones. Other methods you can use include,

  1. db.(collection name).remove({}

In this case, you can use it to delete one specific document, or multiple or all. You will need to pass an empty filter or a specified condition accordingly. However, this method isn’t as approved of in the updated versions.

  1. The Delete Command

Here the usage goes,

db.runCommand(

{

            delete:”Customers”

(Other conditions)

}

However, with this, you can pass multiple criteria in the deletion of documents. The fields include: delete, deletes, writeConcern, ordered, let, q, collation, and so on. However, none would work on capped collections.

Hope this helps you understand the basics of the MongoDB delete document. For further understanding, you can study up the fields involved in the deletion of documents or how to delete according to given behaviour, and so on.

You May Also Like

About the Author: John Vick

Leave a Reply

Your email address will not be published. Required fields are marked *