MongoDB Delete Document from a Collection - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
MongoDB Delete Document from a Collection

MongoDB Delete Document from a Collection

MongoDB Delete Document from a Collection


In this tutorial we will learn how to delete documents 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)
To understand how to specify criteria in MongoDB commands, refer this tutorial: MongoDB Query Document.

Delete Document using remove() method

Lets say I have a collection students in my MongoDB database named mycareerrepublicdb. The documents in students collection are:
> db.students.find().pretty()
{
"_id" : ObjectId("59bcecc7668dcce02aaa6fed"),
"StudentId" : 1001,
"StudentName" : "Steve",
"age" : 30
}
{
"_id" : ObjectId("59bcecc7668dcce02aaa6fef"),
"StudentId" : 3333,
"StudentName" : "Rick",
"age" : 35
}
Now I want to remove the student from this collection who has a student id equal to 3333. To do this I would write a command using remove() method like this:
db.students.remove({"StudentId": 3333})
Output:
WriteResult({ "nRemoved" : 1 })
To verify whether the document is actually deleted. Type the following command:
db.students.find().pretty()
It will list all the documents of students collection.
Here is the screenshot of all the above mentioned steps:

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.
> db.walkingdead.find().pretty()
{
"_id" : ObjectId("59bf280cb8e797a22c654229"),
"name" : "Rick Grimes",
"age" : 32,
"rname" : "Andrew Lincoln"
}
{
"_id" : ObjectId("59bf2851b8e797a22c65422a"),
"name" : "Negan",
"age" : 35,
"rname" : "Jeffrey Dean Morgan"
}
{
"_id" : ObjectId("59bf28a5b8e797a22c65422b"),
"name" : "Daryl Dixon",
"age" : 32,
"rname" : "Norman Reedus"
}
Lets say I want to remove the document that has age equal to 32. 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.walkingdead.remove({"age": 32}, 1)
Output: As you can see only one document got deleted.
WriteResult({ "nRemoved" : 1 })

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({})
If you want to drop the collection along with all the documents then refer this guide: MongoDB Drop Collection

About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.