MongoDB update With Examples

MongoDB update is used to update document in a collection. In last tutorial, we learned about MongoDB insert with Mongo Shell and Java driver. Today we will look into MongoDB update and different options provided by Mongo Shell and Java driver.

First parameter in MongoDB update is query that gives us the target rows – for example {country:"USA"} to get all documents where country is USA and {country:{$ne:"USA"}} to get all the documents where country is not USA.

Second parameter in MongoDB update is used to define the list of fields to update, for example we can use {name:"Pankaj Updated"} to update the name.

We have many mongo update parameter operators available – most widely used are $set, $inc, $currentDate.

We will look into some of these in next sections.

Now let’s see some of the MongoDB update example using Mongo shell.

  • MongoDB Update single field in a single document

    Notice that I am using $set operator to update a field value, if we don’t use that it will replace the whole document, as shown below. We can use it to replace all the fields in a particular document.

  • MongoDB Update multiple fields

    Notice that $set is used with JSON data, so if you want multiple fields to set then we can pass them as JSON.

  • MongoDB Update – Add a new field

    We can use $set operator to add a new field to the document too, as shown below.

  • MongoDB Update subdocument

    We can use mongo update dot operator to update values in a MongoDB subdocument, it’s very similar to java syntax to access methods and variables.

    Notice the use of double quote for key, if double/single quote is not used then it will throw error message as SyntaxError: Unexpected token ..

  • MongoDB Update – Remove a field

    We can use MongoDB update $unset operator to remove a field from the document.

  • MongoDB Update – Insert a new document if no match found

  • MongoDB Update multiple documents

    Above update() has incremented the salary by 1000 for every document where salary is less than 9000.

We looked into different MongoDB document update example using Mongo Shell. Now let’s look at some of the examples using MongoDB java driver.

  • MongoDB Update Java Driver – Update Single Column in a single document

    Below program shows you how to use $set in java program to make sure you are updating a single matching document.

    For simplicity, test data before and after update is added as comments in the program itself.

    MongoDBUpdateExample.java

  • MongoDB Update Java Driver – Update multiple columns

    We can add multiple fields in the MongoDB Update Document to update multiple columns in a single document.

  • MongoDB Update Java Driver – Add a new field in a single document

    Again we can use $set to add a new field in MongoDB document using update query.

  • MongoDB Update Java Driver – Update subdocument

    Just like shell commands, we can use dot operator with $set to update sub-documents using update query.

  • MongoDB Update Java Driver – Remove a field

    We use $unset update option to remove a field from MongoDB document, we can remove fields from subdocument too.

  • MongoDB Update Java Driver – Insert a new document if no match found

    We need to pass upsert parameter value as true for this, an example is shown below.

  • MongoDB Update Java Driver – Update multiple documents

    MongoDB Java Driver API provides updateMulti method that we can use to update multiple documents, in below example we are updating all the documents salary by 1000 where it’s less than 9000. This is also one of the example of $inc update option.

That’s all for MongoDB Update document examples using Mongo shell as well as MongoDB java driver, do let me know if you face any problems with it.

By admin

Leave a Reply