How to delete documents in MongoDB

In this MongoDB tutorial, we are going to learn to “delete the documents in MongoDB” with a few examples. We will cover this by implementing various operations with examples. The following list topics we are going to cover in this tutorial:

  • How to delete the documents in MongoDB query
  • How to delete the documents in MongoDB example
  • How to delete a record from documents in MongoDB
  • How to delete all records from the collection in MongoDB
  • How to delete all records from collection using MongoDB Compass
  • How to delete documents in MongoDB on ubuntu
  • How to delete documents in MongoDB using python

How to delete the documents in MongoDB query

The MongoDB shell provides the following method to delete the documents from a collection.

  • We will the following method to delete a single document
db.collection.deleteOne()
  • We will the following method to delete multiple documents
db.collection.deleteMany()

Also, check: MongoDB find by ID

How to delete the documents in MongoDB example

In this topic, you will learn to delete the documents in MongoDB with the help of an example. Let’s get understand this with the help of an example and delete the documents of the collection.

Example:

The following documents were inside the ‘example1‘ collection:

db.example1.find()
{ "_id" : "101", "name" : "James", "age" : "26", "city" : "United Kingdom" }
{ "_id" : "102", "name" : "Michael", "age" : "27", "city" : "United States of America" }
{ "_id" : "103", "name" : "Robert", "age" : "28", "city" : "United States of America" }

Now, we will apply the below query to delete a single document by using the deleteOne() method.

db.example1.deleteOne({})

The above code will delete the first documents of the ‘example1‘ collection.

Let’s check the output:

How to delete the documents in MongoDB example
How to delete the documents in MongoDB example

We have successfully deleted the first document of the collection.

Read: MongoDB compare two fields

How to delete a record from documents in MongoDB

In this topic, you will learn to delete at most a single document that matches a specified filter. Even though multiple documents may match the specified filter but by using the deleteOne() method will delete only one document that matches a condition.

Example:

The following documents were inserted into the ‘example‘ collection.

db.example.find()
{"_id" : "101", "name" : "James", "age" : "26", "city" : "USA" }
{"_id" : "102", "name" : "Michael", "age" : "27", "city" : "Australia" }
{"_id" : "103", "name" : "Robert", "age" : "28", "city" : "United States" }
{"_id" : "104", "name" : "Peter", "age" : "23", "city" : "Canada" }
{"_id" : "105", "name" : "Noah", "age" : "26", "city" : "Australia" }

After that, we will apply the below query to delete only one document that matches a condition.

db.example.deleteOne( { "city": "Australia" } )

Here, we have used the deleteOne() method to delete the one document where the “city” is “Australia”.

Let’s check the output:

How to delete a record from documents in MongoDB
How to delete a record from documents in MongoDB

As per the defined condition, we have successfully deleted the document of the collection.

Read: How to change collection name in MongoDB

How to delete all records from the collection in MongoDB

In this topic, you will learn to delete all records from the collection. You can define criteria or filters that identify the documents to delete. Now, to delete all the documents that match deletion criteria, pass a filter parameter in the deleteMany() method.

Let’s get understand this with the help of an example and delete all documents that match a condition.

Example:

The following documents were inserted into the ‘example‘ collection.

db.example.find()
{ "_id" : "101", "name" : "James", "age" : "26", "city" : "USA" }
{ "_id" : "102", "name" : "Michael", "age" : "27", "city" : "Australia" }
{ "_id" : "103", "name" : "Robert", "age" : "28", "city" : "United States" }
{ "_id" : "104", "name" : "Peter", "age" : "23", "city" : "Canada" }
{ "_id" : "105", "name" : "Noah", "age" : "26", "city" : "Australia" }

After that, we will apply the below query to delete all documents that match a condition.

db.example.deleteMany( { "city": "Australia" } )

Here, we have used the deleteMany() method to delete all the documents where the “city” is “Australia”.

Let’s check the output:

How to delete all records from the collection in MongoDB
How to delete all records from the collection in MongoDB

As per the defined condition, we have successfully deleted all the documents of the collection.

Read: MongoDB find last inserted document

How to delete all records from collection using MongoDB Compass

In this topic, you will learn to delete all records from the collection using MongoDB Compass. The Compass is a GUI tool for querying, aggregating and analyzing your MongoDB data in a visual environment.

Let’s get understand this with the help of an example and delete all records from the collection using MongoDB Compass.

Example:

You have to follow the below steps to understand the example:

  • Open the MongoDB Compass and connect with the server.
  • Select the existing database and collection where you want to delete the documents.
delete all records from collection using MongoDB Compass
Select the database and collection in MongoDB Compass
  • Here we are using the warehouses collection of the aggregation database.
  • Hover the mouse on the specific document which you want to delete.
How to delete all records from collection using MongoDB Compass
Delete all records from the collection using MongoDB Compass
  • On the right side of the document, some options will be shown to you there click on the Delete Document button.
delete the document from collection using MongoDB Compass
Delete the document from the collection using MongoDB Compass
  • Click on the DELETE button to delete the document.
  • You can also click on the CANCLE button, if you don’t want to delete the document.
delete documents from collection using MongoDB Compass
Delete documents from the collection using MongoDB Compass

We have successfully deleted the document from the collection using MongoDB Compass.

Note: In this way manually you can delete the documents of the collection and if you want to delete all the documents you can drop the collection then all the documents will be deleted.

Read: MongoDB order by date

How to delete documents in MongoDB on ubuntu

In this topic, you will learn to delete the documents in MongoDB on the Ubuntu operating system. Let’s get understand this with the help of an example and delete the documents of the collection.

Example:

The following documents were inserted into the ‘animal‘ collection.

db.animal.find()
{ "_id" : ObjectId("61a9f190f29182ab11b47384"), "subject" : "Joe", "content" : "best friend", "likes" : 60, "year" : 2015, "language" : "english" }
{ "_id" : ObjectId("61a9f190f29182ab11b47385"), "subject" : "Dogs", "content" : "Cats", "likes" : 30, "year" : 2015, "language" : "english" }
{ "_id" : ObjectId("61a9f190f29182ab11b47386"), "subject" : "Cats", "content" : "Rats", "likes" : 55, "year" : 2014, "language" : "english" }
{ "_id" : ObjectId("61a9f190f29182ab11b47387"), "subject" : "Rats", "content" : "Joe", "likes" : 75, "year" : 2014, "language" : "english" }

After that, we will apply the below query to delete documents that match a condition.

db.animal.deleteMany( { "year": 2015 } )

Here, we have used the deleteMany() method to delete all the documents where the “year” is “2015“.

Let’s check the output:

How to delete documents in MongoDB on ubuntu
How to delete documents in MongoDB on ubuntu

As per the defined condition, we have successfully deleted all the documents using ubuntu OS.

Read: MongoDB find string contains

How to delete documents in MongoDB using python

In this topic, you will learn to delete the documents in MongoDB using Python. Let’s get understand this with the help of an example and delete the documents of the collection.

Python provides two methods for deleting the documents:

  • delete_one() method:

We will use delete_one() method to delete one document. Here, the first parameter of the delete_one() method is a query object defining which document to delete.

Note, If the query finds more than one document then only the first occurrence is deleted.

Example:

The following documents were inside the example collection:

db.example.find()
{ "_id" : 1, "name" : "John", "address" : "Highway 37, Canada" }
{ "_id" : 2, "name" : "Peter", "address" : "Lowstreet 27,USA" }
{ "_id" : 3, "name" : "Amy", "address" : "Apple st 652, UK" }
{ "_id" : 4, "name" : "Hannah", "address" : "Mountain 21, Canada" }
{ "_id" : 5, "name" : "Michael", "address" : "Valley 345, Australia" }

The following block of code will use to delete the document, here;

  • First, we have imported the pymongo library
  • After that, we make the connection with the host.
  • And, we have accessed the existing database and collection, mydatabase and example respectively.
  • Now, we have defined the condition where “address” is “Vallet 345, Australia“.
  • After that use the delete_one() method to delete the one document as per the defined condition.
import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["example"]

myquery = { "address": "Valley 345, Australia" }

mycol.delete_one(myquery)

#print the customers collection after the deletion:
for x in mycol.find():
  print(x)

Output:

How to delete one document in MongoDB using python
Delete documents in MongoDB using python

We have successfully deleted one document from the collection as per defined conditions using python.

  • delete_many() method

We will use the delete_many() method to delete more than one document. The parameter of the delete_many() method is a query object defining which documents we want to delete.

Example:

The following documents were inside the example collection:

db.example.find()
{ "_id" : 1, "name" : "John", "address" : "Highway 37, Canada" }
{ "_id" : 2, "name" : "Peter", "address" : "Lowstreet 27,USA" }
{ "_id" : 3, "name" : "Amy", "address" : "Apple st 652, UK" }
{ "_id" : 4, "name" : "Hannah", "address" : "Mountain 21, Canada" }
{ "_id" : 6, "name" : "Jack", "address" : "United States of America" }

The following block of code will use to delete the document, here;

  • First, we have imported the pymongo library
  • After that we make the connection with the host.
  • And, we have accessed the existing database and collection, mydatabase and example respectively.
  • Now, we have defined the condition and use regular expression to delete all documents where “name” is starts with the letter “J”.
  • After that use the delete_many() method to delete all the documents as per the defined condition.
  • Print the deleted document count.
import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["example"]

myquery = { "name": {"$regex": "^J"} }

x = mycol.delete_many(myquery)

print(x.deleted_count, " documents deleted.")

Output:

Delete all documents in MongoDB using python
Delete all documents in MongoDB using python

We have successfully deleted all the documents from the collection as per defined conditions using python.

You may also like to read the following MongoDB tutorials.

I hope you have learned about deleting a collection in MongoDB with a few examples and cover the below topics:

  • How to delete the documents in MongoDB query
  • How to delete the documents in MongoDB example
  • How to delete a record from documents in MongoDB
  • How to delete all records from the collection in MongoDB
  • How to delete all records from collection using MongoDB Compass
  • How to delete documents in MongoDB on ubuntu
  • How to delete documents in MongoDB using python