mongodb how to delete document in a collection ?

The blog provides the steps how to delete documents from collection in mongodb. A record or document in mongodb is removed or deleted using deleteOne() , deleteMany(), remove() and findOneAndDelete() methods which we will discuss in detail with examples.

Delete Document in MongoDB

The below needs to be considered when delete a document in MongoDB

  • MongoDB allows to delete a single document in the collection
  • MongoDB allows to delete multiple documents in the collection
  • MongoDB allows to delete a document by _id
  • MongoDB allows to delete a document based on sort criteria

  MongoDB provides the below given methods for deleting a document in a collection.

S. No Delete Method Delete Method Description
1 db.collection.deleteOne() allows to delete a single document in a collection that matches the delete criteria
2 db.collection.deleteMany() allows to delete multiple document in a collection that matches the delete criteria
3 db.collection.remove() allows to delete a single document or multiple document in a collection that matches the delete criteria
4 db.collection.findOneAndDelete() allows to delete a single document in a collection that matches the delete criteria  and sort criteria

 

Method Name – deleteOne()

Syntax – db.collection.deleteOne(<DELETE_CRITERIA>)

Example: Retrieve the existing documents from the collection oracleappsUsers

> db.oracelappsUsers.find()
{ "_id" : ObjectId("5f13c9df9104ecc5b2149d60"), "name" : "Amit Trivedi", "userType" : "Admin", "description" : "MongoDB insert document sample", "createdBy" : "oracelappshelp.com" }
{ "_id" : ObjectId("5f13ce54a3587b86324785d9"), "name" : "Rohit Verma", "userType" : "Admin", "description" : "MongoDB insert document Tutorial", "createdBy" : "oracelappshelp.com" }
{ "_id" : ObjectId("5f13ce7ca3587b86324785da"), "name" : "Mohit Sharma", "userType" : "Admin", "description" : "MongoDB insert document sample", "createdBy" : "oracelappshelp.com" }

Now, delete a single document by executing the below delete document command

> db.oracelappsUsers.deleteOne( { name: "Rohit Verma" } )
{ "acknowledged" : true, "deletedCount" : 1 }

Now, Retrieve the available documents from the collection – oracleappsUsers. It will list down 2 documents only.

> db.oracelappsUsers.find()
{ "_id" : ObjectId("5f13c9df9104ecc5b2149d60"), "name" : "Amit Trivedi", "userType" : "Admin", "description" : "MongoDB insert document sample", "createdBy" : "oracelappshelp.com" }
{ "_id" : ObjectId("5f13ce7ca3587b86324785da"), "name" : "Mohit Sharma", "userType" : "Admin", "description" : "MongoDB insert document sample", "createdBy" : "oracelappshelp.com" }

Method Name – deleteMany()

Syntax – db.collection.deleteMany(<DELETE_CRITERIA>)

Example: Retrieve the existing documents from the collection oracleappsUsers

> db.oracelappsUsers.find().pretty()
{
"_id" : ObjectId("5f12fd48d4a587e31cddd7e5"),
"name" : "Mohit Sharma",
"userType" : "Administrator",
"description" : "MongoDB insert document sample",
"createdBy" : "oracelappshelp.com"
}
{
"_id" : ObjectId("5f130f1c3a96070d59afd2c0"),
"name" : "Rohit Verma",
"userType" : "Administrator",
"description" : "MongoDB insert document Tutorial",
"createdBy" : "oracelappshelp.com"
}

Now, delete all the documents from the collection where userType =’Admin’ which results into deletion of multiple documents

> db.oracelappsUsers.deleteMany( { userType: 'Administrator' } )
{ "acknowledged" : true, "deletedCount" : 2 }

Now execute the find() to get the document details. There should not be any document available in the collection.

> db.oracelappsUsers.find().pretty()

Method Name – remove()

Syntax – db.collection.remove(<delete_criteria>, justOne)

where

delete_criteria – is the deletion condition that need to be applied to the document

justOne – is the Boolean value when enabled, allows to delete only 1 document based on delete criteria

The remove() allows to delete a single document or multiple document that matches the delete criteria. The remove() returns an object with the status of the operation

Example: Add New records. Retrieve the existing documents from the collection oracleappsUsers

> db.oracelappsUsers.find()
{ "_id" : ObjectId("5f1546f5b5a69e08eec889b5"), "name" : "Mohit Sharma", "userType" : "Administrator", "description" : "MongoDB insert document sample", "createdBy" : "oracelappshelp.com" }
{ "_id" : ObjectId("5f15470db5a69e08eec889b6"), "name" : "Ashish Goyal", "userType" : "Subscriber", "description" : "MongoDB insert document Tutorial", "createdBy" : "oracelappshelp.com" }
{ "_id" : ObjectId("5f154722b5a69e08eec889b7"), "name" : "Praveen Gupta", "userType" : "Subscriber", "description" : "MongoDB insert document Tutorial", "createdBy" : "oracelappshelp.com" }

Now execute the remove() to remove the user with name -“Praveen Gupta”

> db.oracelappsUsers.remove({"name":"Praveen Gupta"})
WriteResult({ "nRemoved" : 1 })

Execute the find() to retrieve the available documents from the collection

> db.oracelappsUsers.find()
{ "_id" : ObjectId("5f1546f5b5a69e08eec889b5"), "name" : "Mohit Sharma", "userType" : "Administrator", "description" : "MongoDB insert document sample", "createdBy" : "oracelappshelp.com" }
{ "_id" : ObjectId("5f15470db5a69e08eec889b6"), "name" : "Ashish Goyal", "userType" : "Subscriber", "description" : "MongoDB insert document Tutorial", "createdBy" : "oracelappshelp.com" }

If we need to remove all the documents from the collection then we can execute the below given command. This method execution is similar to TRUNCATE Table SQL in RDBMS where all records will be removed.

> db.oracelappsUsers.remove({})

Method Name – findOneAndDelete()

Syntax – db.collection.findOneAndDelete(<delete_criteria>, justOne)

The findOneAndDelete() allows to delete the document based on the delete criteria and sort criteria and returns the deleted document

db.collection.findOneAndDelete(
   <filter>,
   {
     projection: <document>,
     sort: <document>,
     maxTimeMS: <number>,
     collation: <document>
   }
)
Parameter Parameter Type Parameter Description
filter Document depicts the delete criteria
projection Document depicts the sub set of fields to return
sort Document specifies the sorting order matched as per the filter.
maxTimeMS Number specifies the time for the operation to complete
collation Document allows to specify language-specific rules for string comparison