MongoDb Tutorial

Day 7 : Update document in MongoDB








Mongodb tutorial series
Overview

In this part of the Learn Mongo Series, we will learn how to update a document in mongodb.

Let's start

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 the first occurrence : Updating using 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"
    }
    >
    
    
    
    										
    									

  • Update all occurrences : If you want to update all the documents matching the 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"
    }
    >
    
    
    
    										
    									

  • Update using 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'
    }
    >
    
    
    
    										
    									

    Now if you will check this document it consists of this new values instead of the old ones which includes name, age and description.
Hurray !! Mission Accomplished. We have successfully updated the documents from mongodb collections.

Summary

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 :

  1. use : This command is use to either create a new database or switch to an already existing database.
  2. db.Collection_name.find().pretty() : This command is used to list all the documents from the collection
  3. 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
  4. 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 .
  5. db.collection_name.save() : This command is used to replace the existing document completely with a new document.