CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Mongo DB > Delete in MongoDB

Delete in MongoDB

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: August 13, 2022 ยท 3 min read ยท 0 Comments
Share: X in ๐Ÿ”—
Delete documents and collections 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

Related Posts:

  • Pagination and Sorting in Spring Boot
  • Sort List of Objects in Java
  • MySQL Commands for Developers
  • Update MongoDB Document
  • MongoDB Document to POJO Class in Java
  • Control Statements in Java
Tags:mongodb
Was this article helpful?
โ† Previous ArticleUpdate MongoDB Document
Next Article โ†’Comparison Operator in MongoDB

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap