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 the update function in a different ways. what are the different ways of update function in MongoDB which is described in this post?

1. 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}])

2. 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

3. 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 }
>

4. 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 }
>

5. 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 } )

6. 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 }})

7. 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.

8. How to add fields in all the documents

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

9. 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}});
})

If you like this article, please comment and share this. Happy Learning! 🙂


Connect with

Leave a Comment

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