Deleting Documents in MongoDB: Easy Guide

This piece talks about deleting documents in MongoDB. Deleting documents in MongoDB can be done in various ways. You may choose to delete a single document or multiple documents from a collection. The delete methods that we will be discussing today are deleteOne() and deleteMany().

In this guide, I will illustrate multiple examples so you can understand how deleting documents in MongoDB works. You will also be able to integrate these methods to work for your application. so, let’s get this article started.

Deleting Documents in MongoDB: Methods Syntaxes

Let us take a quick look at what the methods syntaxes look like for deleting documents in MongoDB:

db.collection_name.deleteOne()
db.collection_name.deleteMany()

Deleting Documents in MongoDB Using deleteOne() and deleteMany()

In this section, we will demonstrate examples of the methods that allow deleting documents in MongoDB. So, let us get started.

Deleting Documents in MongoDB Using deleteOne() Method

  • Start up the MongoDB server
  • Choose the database you want to work in:
show dbs
use dronesDen
  • Let us list all the documents in our collection:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("616659d1ab1b93c0ca076bae"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "FreezweX Dove 89",
        "price" : 50000,
        "weight" : "65 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076baf"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Triple6 TerraHawk 32",
        "price" : 100000,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb0"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sephra Ahnic Eagle - PY6",
        "price" : 100000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb1"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Pingoin Teal robin - Z42",
        "price" : 50000,
        "weight" : "14 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb2"),
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "There’s My Pheonix (Limited Edition) - 52",
        "price" : 100000,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Photography"
                ]
        }
}
  • Let us now use the deleteOne function to delete a single document:
> db.drones.deleteOne( { "_id" : ObjectId("616659d1ab1b93c0ca076bb2") } )
{ "acknowledged" : true, "deletedCount" : 1 }
  • Let us take a look at our documents:
> db.drones.find({}).pretty()
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bae"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "FreezweX Dove 89",
        "price" : 50000,
        "weight" : "65 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076baf"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Triple6 TerraHawk 32",
        "price" : 100000,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb0"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sephra Ahnic Eagle - PY6",
        "price" : 100000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb1"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Pingoin Teal robin - Z42",
        "price" : 50000,
        "weight" : "14 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Recreation"
                ]
        }
}

The deleteOne() function had been successful at deleting documents in MongoDB.

Deleting Documents in MongoDB Using deleteMany() Method

> db.drones.deleteMany( { price : 50000 } )
{ "acknowledged" : true, "deletedCount" : 2 }
  • Checking what our documents look like:
> db.drones.find({}).pretty()
{
        "_id" : ObjectId("616659d1ab1b93c0ca076baf"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Triple6 TerraHawk 32",
        "price" : 100000,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb0"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sephra Ahnic Eagle - PY6",
        "price" : 100000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}

The deleteMany() function had been successful at deleting documents in MongoDB.

Read More: Comprehensive Guide: Remove a Field Completely from a MongoDB Document

Conclusion

MongoDB is a cutting-edge database management system with several features and functionalities. Because of its ease of use and powerful capabilities, MongoDB is popular among novices.

Deleting documents in MongoDB can be done in various ways. You may choose to delete a single document or multiple documents from a collection. The delete methods that we have discussed today are deleteOne() and deleteMany().

Noteworthy References

MongoDB

Aneesha S
Aneesha S
Articles: 172