Delete Document from a Collection – MongoDB

MongoDB Delete Document from a Collection

The remove() method is used for removing the documents from a collection in MongoDB.

Syntax of remove() method:

db.collection_name.remove(delete_criteria)

Delete Document using remove() method

Lets say I have a collection ‘department’ in my MongoDB database named ’employeeDB’. The documents in ‘department’ collection are:


db.department.find()
{ "_id" : ObjectId("5b3725145eb29c373ddadd52"), "dept_id" : 3, "name" : "Production" }
{ "_id" : ObjectId("5b3727195eb29c373ddadd56"), "dept_id" : 2, "name" : "Finance" }
{ "_id" : ObjectId("5b3bcdb46728a1b55ecec860"), "dept_id" : 4, "name" : "Sales" }
{ "_id" : ObjectId("5b3bcdba6728a1b55ecec861"), "dept_id" : 5, "name" : "Testing" }
{ "_id" : ObjectId("5b3bced56728a1b55ecec862"), "dept_id" : 5, "name" : "Pre-Sales" }

Now I want to remove the department from this collection who has a dept_id equal to 2. To do this I would write a command using remove() method like this:

db.department.remove({"dept_id": 2})

OutPut :

WriteResult({ "nRemoved" : 1 })

To verify whether the document is actually deleted. Type the following command:

db.department.find()

{ "_id" : ObjectId("5b3725145eb29c373ddadd52"), "dept_id" : 3, "name" : "Production" }
{ "_id" : ObjectId("5b3bcdb46728a1b55ecec860"), "dept_id" : 4, "name" : "Sales" }
{ "_id" : ObjectId("5b3bcdba6728a1b55ecec861"), "dept_id" : 5, "name" : "Testing" }
{ "_id" : ObjectId("5b3bced56728a1b55ecec862"), "dept_id" : 5, "name" : "Pre-Sales" }

How to remove only one document matching your criteria?

When there are more than one documents present in collection that matches the criteria then all those documents will be deleted if you run the remove command. However there is a way to limit the deletion to only one document so that even
if there are more documents matching the deletion criteria, only one document will be deleted.

db.collection_name.remove(delete_criteria, justOne)

Here justOne is a Boolean parameter that takes only 1 and 0, if you give 1 then it will limit the the document deletion to only 1 document. This is an optional parameters as we have seen above that we have used the remove() method without
using this parameter.

For example I have the following records in collection.


{ "_id" : ObjectId("5b3725145eb29c373ddadd52"), "dept_id" : 3, "name" : "Production" }
{ "_id" : ObjectId("5b3bcdb46728a1b55ecec860"), "dept_id" : 4, "name" : "Sales" }
{ "_id" : ObjectId("5b3bcdba6728a1b55ecec861"), "dept_id" : 5, "name" : "Testing" }
{ "_id" : ObjectId("5b3bced56728a1b55ecec862"), "dept_id" : 5, "name" : "Pre-Sales" }

Lets say I want to remove the document that has dept_id equal to 5. There are two documents in this collection that are matching this criteria. However to limit the deletion to one we are setting justOne parameter to true.

db.department.remove({"dept_id": 5},1)

Output: As you can see only one document got deleted.

WriteResult({ "nRemoved" : 1 })

OutPut :


db.department.find()
{ "_id" : ObjectId("5b3725145eb29c373ddadd52"), "dept_id" : 3, "name" : "Production" }
{ "_id" : ObjectId("5b3bcdb46728a1b55ecec860"), "dept_id" : 4, "name" : "Sales" }
{ "_id" : ObjectId("5b3bced56728a1b55ecec862"), "dept_id" : 5, "name" : "Pre-Sales" }

Remove all Documents

If you want to remove all the documents from a collection but does not want to remove the collection itself then you can use remove() method like this:

db.collection_name.remove({})

Leave a Reply

Your email address will not be published. Required fields are marked *