Delete in MongoDB

In this post, we will learn to use the delete command in MongoDB. As we all know, having a delete functionality is very important in any system so that we can remove unwanted data when required.

Delete MongoDB Collection

To delete a collection in MongoDB we can use a drop() method.

Syntax:

db.<collectionName>.drop()

Example:

To delete a collection first we have to go inside the database where the collection is present by using the command:

use myDb

It switches to the myDb database.

Let’s say, we want to delete a collection name employee then we can use the following command to delete it.

db.employee.drop()

The above command will delete a collection name employee.

Delete MongoDB Document

There are two methods to delete documents in MongoDB. These are:

  • deleteOne()
  • deleteMany()

deleteOne() method in MongoDB

The deleteOne() method is the best choice when deleting a document by its unique field like _id. It makes sure to delete a single document that matches the field _id.

Example:

db.employee.deleteOne({"_id": ObjectId("62f27f3587f66ea65f655524")})

The command above will delete a document that matches the given ObjectId.

Though the field _id is preferred to use while using deleteOne() method. We can still use a deleteOne() method to delete by other fields.

Let’s consider the following documents:

{
        "_id" : ObjectId("62f2806287f66ea65f655525"),
        "name" : "Radha Krishnan",
        "department" : "HR",
        "salary" : 55000,
        "address" : {
                "city" : "Pokhara",
                "area" : "Lakeside"
        }
}
{
        "_id" : ObjectId("62f72f0ec41dfbf8c1e24e15"),
        "name" : "Coder Sathi",
        "department" : "Engineering",
        "salary" : 50000,
        "address" : {
                "city" : "Kathmandu",
                "area" : "Pashupatinath"
        }
}
{
        "_id" : ObjectId("62f72f14c41dfbf8c1e24e16"),
        "name" : "Radha Krishnan",
        "department" : "HR",
        "salary" : 50000,
        "address" : {
                "city" : "Pokhara",
                "area" : "Lakeside"
        }
}

We have three documents. Let’s delete a document that matches the name Radha Krishnan using the command below.

db.employee.deleteOne({"name": "Radha Krishnan"})

This command deletes the first document that matches the name Radha Krishnan although there are two documents with that name.

If we execute the find command, we can see that there are two documents available. See the below:

{
        "_id" : ObjectId("62f72f0ec41dfbf8c1e24e15"),
        "name" : "Coder Sathi",
        "department" : "Engineering",
        "salary" : 50000,
        "address" : {
                "city" : "Kathmandu",
                "area" : "Pashupatinath"
        }
}
{
        "_id" : ObjectId("62f72f14c41dfbf8c1e24e16"),
        "name" : "Radha Krishnan",
        "department" : "HR",
        "salary" : 50000,
        "address" : {
                "city" : "Pokhara",
                "area" : "Lakeside"
        }
}

Hence, when using deleteOne() method without _id field to delete documents in MongoDB it only deletes a single document no matter how many documents it matches with the given parameter.

deleteMany() method in MongoDB

The deleteMany() method can be used to delete one or more documents at a time.

Syntax:

db.employee.deleteMany(criteria);

Let’s delete all the document that matches the name Radha Krishnan from the employee collection below:

{
        "_id" : ObjectId("62f2806287f66ea65f655525"),
        "name" : "Radha Krishnan",
        "department" : "HR",
        "salary" : 55000,
        "address" : {
                "city" : "Pokhara",
                "area" : "Lakeside"
        }
}
{
        "_id" : ObjectId("62f72f0ec41dfbf8c1e24e15"),
        "name" : "Coder Sathi",
        "department" : "Engineering",
        "salary" : 50000,
        "address" : {
                "city" : "Kathmandu",
                "area" : "Pashupatinath"
        }
}
{
        "_id" : ObjectId("62f72f14c41dfbf8c1e24e16"),
        "name" : "Radha Krishnan",
        "department" : "HR",
        "salary" : 50000,
        "address" : {
                "city" : "Pokhara",
                "area" : "Lakeside"
        }
}

Example:

db.employee.deleteMany({"name": "Radha Krishnan"});

After executing the above command it deletes all the documents that match the name Radha Krishnan.

We can see the remaining documents using the find command below:

{
        "_id" : ObjectId("62f72f0ec41dfbf8c1e24e15"),
        "name" : "Coder Sathi",
        "department" : "Engineering",
        "salary" : 50000,
        "address" : {
                "city" : "Kathmandu",
                "area" : "Pashupatinath"
        }
}

Conclusion

In this post, we learned to delete documents in MongoDB using deleteOne() and deleteMany() methods. We also learned when to use deleteOne() and when to use deleteMany() methods with example

Sharing Is Caring:
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments