In this part of the Learn Mongo Series, we will learn how to delete a document in mongodb.
We can delete a document in mongodb using the remove()
command. remove()
method in mongodb takes 2 parameters which are explained below :
1
or true
, then only the first occurrence of the documents matching the deletion criteria
is deleted.
db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
There are 3 ways to remove the documents using the remove()
method which are :
> use students
switched to db students
> db.details.find().pretty()
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1c"),
"name" : "nodejsera",
"age" : "10",
"description" : "Mongodb tuttorial series"
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1e"),
"name" : "b",
"age" : "12",
"description" : "something "
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1d"),
"age" : "11",
"description" : "learn mongodb",
"name" : "a"
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1f"),
"name" : "nodejsera",
"age" : "13",
"description" : "Express tutorial series"
}
>
db.collection_name.remove()
command as shown below :
> use students
switched to db students
> db.details.remove()
>
details
collection.
deletion_Criteria
: If we want to remove all the documents which matches a certain
criteria, then we can use db.collection_name.remove(Deletion_criteria)
command as shown below :
> use students
switched to db students
> db.details.remove({'name':'nodejsera'})
> db.details.find().pretty()
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1e"),
"name" : "b",
"age" : "12",
"description" : "something "
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1d"),
"age" : "11",
"description" : "learn mongodb",
"name" : "a"
}
>
deletion criteria
: If we want to delete the first
occurrence of the document matching the deletion criteria , then we can use db.collection_name.remove(Deletion_criteria,1)
command as shown below :
> use students
switched to db students
> db.details.remove({'name':'nodejsera'},1)
> db.details.find().pretty()
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1e"),
"name" : "b",
"age" : "12",
"description" : "something "
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1d"),
"age" : "11",
"description" : "learn mongodb",
"name" : "a"
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1f"),
"name" : "nodejsera",
"age" : "13",
"description" : "Express tutorial series"
}
>
In this part of learn mongo series , we learned about how we can remove documents from mongodb collection. We learned the following commands of mongodb :
use :
This command is use to either create a new database or switch to an already existing database. db.Collection_name.find().pretty() :
This command is used to list all the documents from the collection db.Collection_name.remove() :
This command is used to remove all the documents from the collection. db.Collection_name.remove({Deletion_Criteria}) :
This command is used to remove all the documents from the collection which matches deletion criteria. db.Collection_name.remove({Deletion_Criteria},1) :
This command is used to remove the first occurrence of the document from the collection whcih macthes
deletion criteria.