New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

MongoDB with Java: Updating Document

While updating data in document present in any collection of our database, we will come across various scenarios, let us analyze them step by step.


Updating a single field value

We will update the field name and change its value to ABC. To do so, we will have to first find the correct document to update the value, which we can do using some known field. In our case we will find the document with _id value xyz123 which will act as condition in our query and then we will update the value of the name field.

package com.mongo;

import com.mongodb.DB; 
import com.mongodb.MongoClient; 
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;

public class MakeConnection { 
	public static void main(String[] args) {
		try { 
			// code to create the connection
			MongoClient mongoClient = new MongoClient("localhost", 27017); 
			// code to connect to the database
			DB db = mongoClient.getDB("DemoDB");

			// get the User collection from DB
			DBCollection userCollection = db.getCollection("User");

			// condition query to get document which has to be updated
			BasicDBObject queryObj = new BasicDBObject();
			queryObj.put("_id", "xyz123");

			// create new document with data to be updated
			BasicDBObject newObj = new BasicDBObject();
			newObj.put("name", "ABC");

			// updating data into collection
			userCollection.update(queryObj, newObj);
		} 
		catch(Exception e) { 
			e.printStackTrace(); 
		} 
	}
}

In the above case the existing document will be completely replace, which means that if it had some other fields other than the name field, then those fields will get removed. But we don't want that, right. We want to be able to just update a single value in any document, in that case we should use update modifier $set.

The $set update modifier is a keyword which informs mongo that only the provided field should be updated in a document

Let's update the above code:

package com.mongo;

import com.mongodb.DB; 
import com.mongodb.MongoClient; 
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;

public class MakeConnection { 
	public static void main(String[] args) {
		try { 
			// code to create the connection
			MongoClient mongoClient = new MongoClient("localhost", 27017); 
			// code to connect to the database
			DB db = mongoClient.getDB("DemoDB");

			// get the User collection from DB
			DBCollection userCollection = db.getCollection("User");

			// condition query to get document which has to be updated
			BasicDBObject queryObj = new BasicDBObject();
			queryObj.put("_id", "xyz123");

			// create new document with data to be updated
			BasicDBObject newObj = new BasicDBObject();
			newObj.put("$set", new BasicDBObject.append("name", "ABC"));

			// updating data into collection
			userCollection.update(queryObj, newObj);
		} 
		catch(Exception e) { 
			e.printStackTrace(); 
		} 
	}
}

The code above presents the correct way for updating any document. The output of the above program will be:

Data Updation in MongoDB

The value of field name is updated from xyz to ABC.

In the above case the existing document is


Updating an Array in a Document field

Considering a scenario, where we have an array for field friends

Data Updation in MongoDB


and if we want to update that array by adding values, we can do it by referring the below code snippet:

Data Updation in MongoDB


and Amit is added to the array against field friends.

Data Updation in MongoDB


Updating an Object in a Document field

We can even add object to the array of object using update query. For an example, for against field comments, if we want to add multiple objects with various field, it is possible.

Here is the code snippet:

Data Updation in MongoDB


and the corresponding output is:

Data Updation in MongoDB