How to update data in mongoDB?

Connect with

How to Update data in MongoDB using mongo shellHow to update data in MongoDB using mongo shell. Learn different update query in mongoDB, mongodb update example using mongo shell using db.collection.update() function.

1. How to Update data in MongoDB using mongo shell

In this article, I explained, how to update data in MongoDB on mongo shell using different option in command db.collection.update(). You can update documents in MongoDB using the update function in different ways. what are the different ways of update functions in MongoDB that are described in this post?

2. Pre-requisite for update of data in mongoDB

You must have some document in a collection to update, if you donot have mongoDB installed on your machine, then visit Install mongoDB 3.2 on Windows 10 or Install mongoDB 3.2 on Linux and add document with basic set of data to run update query.

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

3. Syntax of updating data in MongoDB

The db.collection.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

4. Get inserted Data

This is just to check documents are in collection or not for running of MongoDB update example.

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

5. Update query in MongoDB for _id

Update query in mongoDB for _id field, 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 }
>

6. Update multiple document using multi : true

If we wanted to update all matching documents that meet criteria. This is MongoDB update example for all matching documents.

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

7. Update query in MongoDB using $set function

In this MongoDB update example, the $set use to add additional fields in the document.

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

8. Update query with $inc

The $inc modifier can be used to increment the value of an existing field to update data in mongodb.

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

add state=Delhi in all the documents.

9. Add fields in all the documents using $set

Update query in MongoDB using $set. Following is the example of mongoDB update.

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

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

11. Reference

You can visit docs.mongodb.com for more details.

Thanks for visiting this post for MongoDB update example on mongo shell. You can also visit MongoDB Tutorial Listing page for more articles on MongoDB document-oriented database.
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 *