How to update data in mongoDB?

Connect with

MongoDB
In this article I tried to explain how to update data in mongoDB in different options available in mongodb. You can update documents in mongodb using update function in different way. what are the different way of update function in mongoDB which is described in this post.

Pre-requisite for update of data in mongoDB

installationo of mongoDB and addition of following basic data to update on.

db.user.insert({"userid":"ranjeet","city":"new delhi", "age":30})
db.user.insert({"userid":"rk","city":"new delhi", "age":25})
db.user.insert([{"userid":"anushka","city":"mumbai", "age":27},{"userid":"dan","city":"chennai", "age":35}])

Syntax of updating data in MongoDB

The update( ) function modifies one or more documents from a collection of database.

 db.collectionName.update({queryCriteria}, {jsonData}, {upsert, multi})

Here, description of above syntax as:

  • db: here db is the variable reference pointer of data base which you switched to. in my case mysoftkeyDB. Identify which database you want to update.
  • collection: name of collection in which you want to update operation.
  • parameters: update function accept query and jsonData, third parameter is optional.
  • queryCriteria: specify query to match the document so that for those will update.
  • jsonData: provide the document which you want to update

Get inserted Data

> db.user.find();
{ "_id" : ObjectId("58399c5f682514efb3474297"), "userid" : "ranjeet", "city" : "new delhi", "age" : 30 }
{ "_id" : ObjectId("58399c6c682514efb3474298"), "userid" : "rk", "city" : "new delhi", "age" : 25 }
{ "_id" : ObjectId("58399c96682514efb3474299"), "userid" : "anushka", "city" : "mumbai", "age" : 27 }
{ "_id" : ObjectId("58399c96682514efb347429a"), "userid" : "dan", "city" : "chennai", "age" : 35 }
>

Update Document by _id

following update function will replace existing document with provided document for the _id matching document.

> db.user.update({"_id": ObjectId("58399c5f682514efb3474297")}, {city:"New Delhi"});

Console Output:

> db.user.update({"_id": ObjectId("58399c5f682514efb3474297")}, {city:"New Delhi"});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : ObjectId("58399c5f682514efb3474297"), "city" : "New Delhi" }
{ "_id" : ObjectId("58399c6c682514efb3474298"), "userid" : "rk", "city" : "new delhi", "age" : 25 }
{ "_id" : ObjectId("58399c96682514efb3474299"), "userid" : "anushka", "city" : "mumbai", "age" : 27 }
{ "_id" : ObjectId("58399c96682514efb347429a"), "userid" : "dan", "city" : "chennai", "age" : 35 }
>

How to update using multi:true options

If we wanted to update all documents that meet this criteria as:

db.book.update( { city : "new delhi" }, { "state": "Delhi"}, { multi: true } )

How to use $set in update function

The $set use to add additional fields in the document.

db.user.update ( { "userid" : "ranjeet" }, { "$set": { "sal":2000, sessionCounter:1 }})

How to use $inc in update

The $inc modifier can be used to increment the value of an existing field

db.user.update ( { "userid" : "ranjeet" }, {"$inc" : {"sessionCounter":1 }})

add state=Delhi in all the documents.

How to add fields in all the documents

db.user.update ( {}, {"$set" : {"state":"Delhi" }})

How to add fields in all the documents using forEach

forEach can be used to manipulate the document of mongoDB. you can go another article: How to How to user forEach loop in mongoDB to manipulate document


db.user.find().forEach(function(doc){
   var updatedSal = doc.sal + (doc.sal * 10/100);
    db.user.update({_id: doc._id},{$set:{"sal": updatedSal}});
})

Connect with

Leave a Reply

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