In this part of the Learn Mongo Series, we will learn how to update a document in mongodb.
We can update a document in mongodb using the update()
and save()
method.
Consider the following dataset :
>use demo_db
switched to db demo_db
>db.details.find().pretty()
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1c"),
"name" : "nodejsera",
"age" : "10",
"description" : "Mongodb tutorial series"
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1d"),
"name" : "a",
"age" : "11",
"description" : "learn mongodb"
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1e"),
"name" : "b",
"age" : "12",
"description" : "something "
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1f"),
"name" : "nodejsera",
"age" : "13",
"description" : "express tutorial"
}
>
update()
method. Ths syntax is db.collection_name.update({search_criteria},{$set:{updated_data}})
.
Example is given below :
> db.details.update({'name':'a'},{$set:{'name' : 'updated a'}})
> db.details.find().pretty()
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1c"),
"name" : "nodejsera",
"age" : "10",
"description" : "Mongodb tutorial series"
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1e"),
"name" : "b",
"age" : "12",
"description" : "something "
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1d"),
"age" : "11",
"description" : "learn mongodb",
"name" : "Updated a"
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1f"),
"name" : "nodejsera",
"age" : "13",
"description" : "express tutorial"
}
>
search_criteria
then it can be done as follows :
> db.details.update({'name':'nodejsera'},{$set:{'name' : 'c'}},{multi:true})
> db.details.find().pretty()
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1c"),
"name" : "c",
"age" : "10",
"description" : "Mongodb tutorial series"
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1e"),
"name" : "b",
"age" : "12",
"description" : "something "
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1d"),
"age" : "11",
"description" : "learn mongodb",
"name" : "Updated a"
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1f"),
"name" : "c",
"age" : "13",
"description" : "express tutorial"
}
>
save()
method : It will replace the existing document completely with a new document. the syntax and example is shown below :
>use demo_db
switched to db demo_db
>db.demo_collection.save(
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1f"),
'name':'sophia',
'web' : 'www.nodejsera.com'
})
> db.details.find().pretty()
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1c"),
"name" : "c",
"age" : "10",
"description" : "Mongodb tutorial series"
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1e"),
"name" : "b",
"age" : "12",
"description" : "something "
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1d"),
"age" : "11",
"description" : "learn mongodb",
"name" : "Updated a"
}
{
"_id" : ObjectId("5996be4fa4b26dc0d6d75c1f"),
'name':'sophia',
'web' : 'www.nodejsera.com'
}
>
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.update({search_criteria},{$set:{updated_data}})
This command is used to update the first occurrence of the document data matching the search_criteria
db.collection_name.update({search_criteria},{$set:{updated_data}},{multi: true}) :
This command is used to update the all the occurrences of the document data matching the search_criteria
.
db.collection_name.save() :
This command is used to replace the existing document completely with a new document.