The update() method is the primary method used to modify documents in a MongoDB collection. By default, the update() method updates a single document, but by using the multi option, update() can update all documents that match the query criteria in the collection. The update() method can either replace the existing document with the new document or update specific fields in the existing document.
The update() has the following syntax:
db.collection.update( <query>, <update>, <options> )
The update() method corresponds to the UPDATE operation in SQL, and:
Click here to know more about the Update command and its options.
In Java MongoDB API, you can use collection.update() to update an existing document. Here is some common use cases :
1. A normal way to update an existing document.
Find empId = 10006, and update it with a new document.
BasicDBObject newDocument = new BasicDBObject(); newDocument.put("empId", 10006); newDocument.put("empName", "Dinesh Rajput"); newDocument.put("salary", 70000); collection.update(new BasicDBObject().append("empId", 10006), newDocument);
2. This example show the use of “$inc” modifier to increase a particular value.
Find empId = 10006, update the “salary” value by increasing its value from 70000 to 90000, (70000 + 20000) = 90000.
BasicDBObject newDocument = new BasicDBObject().append("$inc", new BasicDBObject().append("salary", 20000)); collection.update(new BasicDBObject().append("empId", 10006), newDocument);
3. This example show the use of “$set” modifier to update a particular value.
Find empId = 10006, update the “salary” from 70000 to 90000.
BasicDBObject newDocument = new BasicDBObject().append("$set", new BasicDBObject().append("salary", 90000)); collection.update(new BasicDBObject().append("empId", 10006), newDocument);
4. This example show the use of “multi” parameter to update a set of matched documents.
Find empAge= 26, update all the matched documents, “salary” value to 60000.
//find empAge= 26 , update all matched documents , "salary" value to 60000 BasicDBObject updateQuery = new BasicDBObject().append("$set", new BasicDBObject().append("salary", "60000")); //both methods are doing the same thing. //collection.updateMulti(new BasicDBObject().append("empAge", "26"), updateQuery); collection.update(new BasicDBObject().append("empAge", "26"), updateQuery, false, true);
See the full example and its output.
package com.dineshonjava.mongo.test; import java.net.UnknownHostException; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.Mongo; import com.mongodb.MongoException; /** * @author Dinesh Rajput * */ public class UpdateDocumentDemo { public static void printAllDocuments(DBCollection collection){ DBCursor cursor = collection.find(); while (cursor.hasNext()) { System.out.println(cursor.next()); } } public static void removeAllDocuments(DBCollection collection){ collection.remove(new BasicDBObject()); } public static void insertDummyDocuments(DBCollection collection){ BasicDBObject document = new BasicDBObject(); document.put("empId", "10006"); document.put("empName", "Dinesh"); document.put("salary", 70000); BasicDBObject document2 = new BasicDBObject(); document2.put("empId", "10007"); document2.put("empName", "Sweety"); document2.put("salary", 70000); BasicDBObject document3 = new BasicDBObject(); document3.put("empId", "10008"); document3.put("empName", "Dinesh"); document3.put("salary", 60000); collection.insert(document); collection.insert(document2); collection.insert(document3); } public static void main(String[] args) { try { Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("dineshonjavaDB"); // get a single collection DBCollection collection = db.getCollection("employee"); System.out.println("Testing 1..."); insertDummyDocuments(collection); //find empId = 10006, and update it with new document BasicDBObject newDocument = new BasicDBObject(); newDocument.put("empId", "10006"); newDocument.put("empName", "Dinesh Rajput"); newDocument.put("salary", 80000); collection.update(new BasicDBObject().append("empId", "10006"), newDocument); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("Testing 2..."); insertDummyDocuments(collection); //find empId = 10006 and increase its "salary" value by 90000 BasicDBObject newDocument2 = new BasicDBObject().append("$inc", new BasicDBObject().append("salary", 20000)); collection.update(new BasicDBObject().append("empId", "10006"), newDocument2); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("Testing 3..."); insertDummyDocuments(collection); //find empId = 10006 and update salary to from 70000 to 90000 BasicDBObject newDocument3 = new BasicDBObject().append("$set", new BasicDBObject().append("salary", 90000)); collection.update(new BasicDBObject().append("empId", "10006"), newDocument3); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("Testing 4..."); insertDummyDocuments(collection); //find empName = Dinesh , update all matched documents , salary value to 80000 BasicDBObject updateQuery = new BasicDBObject().append("$set", new BasicDBObject().append("salary", 80000)); //both method are same //collection.updateMulti(new BasicDBObject().append("empName", "Dinesh"), updateQuery); collection.update(new BasicDBObject().append("empName", "Dinesh"), updateQuery, false, true); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("Done"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } } }
if every thing fine then run as java application and see the follwing output on the console.
References
Download Source Code + Libs
MongoDBUpdateDemo.zip
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…