Removing documents in MongoDB

Introduction

To continue my series on the CRUD (Create, Read, Update, Delete) API in MongoDB, we will talk about the ‘D’, deleting documents.

Note: I will be using the MongoDB shell in my terminal to show some examples. If you do not have it installed, please refer to my Quick start with MongoDB article

As always, MongoDB has explicit methods names to describe its actions. So, to delete documents from a collection, we have two methods: deleteOne() and deleteMany(). Both of these methods take a filter as their first parameter. Let’s see these in action:

 

deleteOne

First, let me show you my database content:

> db.fruits.find()
{ "_id" : 1, "name" : "apple" }
{ "_id" : 2, "name" : "orange" }
{ "_id" : 3, "name" : "strawberry" }
{ "_id" : 4, "name" : "pear" }

 

Four different documents with an _id and a name field. I want to remove the document with the “_id” : 3. Here is the filter I would give to my deleteOne method:

> db.fruits.deleteOne({"_id": 3})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.fruits.find()
{ "_id" : 1, "name" : "apple" }
{ "_id" : 2, "name" : "orange" }
{ "_id" : 4, "name" : "pear" }

 

My document is gone! Now, what if I use deleteOne but my filter matches several documents in my collection? Well, MongoDB will delete the first one the matches your filter. Be careful when this happens! The document found in first might be based on the order in which they were inserted, the updates made to the documents or the indexes.  Make sure you understand which one applies to you and if it suits your needs.

 

deleteMany

 

To delete several documents, we use the deleteMany() method. Let’s take a look at my fruits collection again:

> db.fruits.find()
{ "_id" : 1, "name" : "apple" }
{ "_id" : 2, "name" : "orange" }
{ "_id" : 4, "name" : "pear" }
{ "_id" : 3, "name" : "orange" }
{ "_id" : 5, "name" : "lemon" }
{ "_id" : 7, "name" : "watermelon" }

I need to remove all the documents with the name field equal to orange. Here is the filter I should use:

> db.fruits.deleteMany({"name": "orange"})
{ "acknowledged" : true, "deletedCount" : 2 }
> db.fruits.find()
{ "_id" : 1, "name" : "apple" }
{ "_id" : 4, "name" : "pear" }
{ "_id" : 5, "name" : "lemon" }
{ "_id" : 7, "name" : "watermelon" }

And there you go, all my oranges are gone!

 

drop

We have one last method to look at, the drop() method. It is used to remove all documents from a collection. You can use deleteMany() to remove all documents by given an empty object as a filter:

> db.fruits.deleteMany({})
{ "acknowledged" : true, "deletedCount" : 4 }

However, it is faster to drop the entire collection.

> db.fruits.drop()
true

Nice and easy, your collection is now empty.

Remember that every operation where you delete data is irreversible. You can’t undo the previous operations, short of using a backup version of your database.

Well, that’s it for deleting documents in a MongoDB collection. I hope it was clear and helpful.

Feel free to share and comment.

Have a nice day.

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

*