findOneAndDelete Function in MongoDB: Ultimate 2021 Guide

FindOneAndDelete Function Featured Image

Today, we’ll discuss more on the findOneAndDelete function in MongoDB. The findOneAndDelete function deletes a single document from a collection based on the selection criteria.

The first document matching the filter query expression is removed from the collection. Among the five parameters, the first is the selection criteria which is required, while the others are optional. After a document is deleted successfully, this function returns that deleted document.

This guide shall cite different examples for all the possible use cases of the findOneAndDelete function in MongoDB.

The findOneAndDelete Function Syntax in MongoDB

Let us take a quick look at what the findOneAndDelete function syntax looks like:

db.collection.findOneAndDelete( filter, options )

Using the findOneAndDelete Function in MongoDB

Let us start using the findOneAndDelete function with a different set of examples.

Deleting a Document with a Criteria Using findOneAndDelete Function

In this example, I will find and delete the first document that matches the given criteria.

  • Run the MongoDB server
  • Choose the database you want to work with and navigate into it:
show dbs
use dronesGala
  • Before we start, let us give a glance to all the available documents in our collection:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("615f1672a7c6532312e3a41d"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Aurelize Frinworks Hawk 7",
        "price" : 12000,
        "weight" : "34 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f16eca7c6532312e3a41e"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Poka Moonskull 2X67",
        "price" : 76400,
        "weight" : "48 kilograms",
        "additionalDetails" : {
                "material" : "nylon",
                "moreUses" : [
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jade Balestrom RW",
        "price" : 24500,
        "weight" : "27 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("615f1879a7c6532312e3a420"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue"
        ],
        "onSale" : false,
        "name" : "VarX Lourra VQ (Limited Edition)",
        "price" : 49500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615f1922a7c6532312e3a421"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Mellori Styxe 175",
        "price" : 15000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6160a831821fd04db70117a7"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Wenderman’s X-copter B32",
        "price" : 19500,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6160a85a821fd04db70117a8"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "GarX Jerrom BQ (Limited Edition)",
        "price" : 39500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
  • Finding and deleting a document with a specific criteria:
> db.drones.findOneAndDelete({name:"Poka Moonskull 2X67"})

{
        "_id" : ObjectId("615f16eca7c6532312e3a41e"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Poka Moonskull 2X67",
        "price" : 76400,
        "weight" : "48 kilograms",
        "additionalDetails" : {
                "material" : "nylon",
                "moreUses" : [
                        "Recreation"
                ]
        }
}
  • Let us take a look at our collection again after the operation:
> db.drones.find({}).pretty()
{
        "_id" : ObjectId("615f1672a7c6532312e3a41d"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Aurelize Frinworks Hawk 7",
        "price" : 12000,
        "weight" : "34 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jade Balestrom RW",
        "price" : 24500,
        "weight" : "27 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("615f1879a7c6532312e3a420"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue"
        ],
        "onSale" : false,
        "name" : "VarX Lourra VQ (Limited Edition)",
        "price" : 49500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615f1922a7c6532312e3a421"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Mellori Styxe 175",
        "price" : 15000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6160a831821fd04db70117a7"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Wenderman’s X-copter B32",
        "price" : 19500,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6160a85a821fd04db70117a8"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "GarX Jerrom BQ (Limited Edition)",
        "price" : 39500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}

We can see that the first document that matched the specified criteria, has been deleted with the findOneAndDelete function.

Deleting a Document with a Sort Criteria Using findOneAndDelete Function

In this example, we will delete the first document that matches our specified sort criteria using the findOneAndDelete function in MongoDB.

Also read: findOne Function in MongoDB: Basic Guide

> db.drones.findOneAndDelete( { price: 39500 }, { sort: { price: -1 } } )

{
        "_id" : ObjectId("6160a85a821fd04db70117a8"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "GarX Jerrom BQ (Limited Edition)",
        "price" : 39500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
  • Checking all our documents after the operation:
> db.drones.find({}).pretty()
{
        "_id" : ObjectId("615f1672a7c6532312e3a41d"),
        "utility" : [
                "Delivery",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Aurelize Frinworks Hawk 7",
        "price" : 12000,
        "weight" : "34 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("615f17a5a7c6532312e3a41f"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jade Balestrom RW",
        "price" : 24500,
        "weight" : "27 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("615f1879a7c6532312e3a420"),
        "utility" : [
                "Monitoring or Inspection",
                "Videography",
                "Combat",
                "Rescue"
        ],
        "onSale" : false,
        "name" : "VarX Lourra VQ (Limited Edition)",
        "price" : 49500,
        "weight" : "19 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615f1922a7c6532312e3a421"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Mellori Styxe 175",
        "price" : 15000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6160a831821fd04db70117a7"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Wenderman’s X-copter B32",
        "price" : 19500,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}

The findOneAndDelete function has successfully deleted the first document that matched our specified descending sort criteria.

Read More: How to Connect MongoDB on Localhost 27017 in Windows – Starter’s Guide

Noteworthy References

MongoDB