Java MongoDB : Update document
In Java MongoDB API, you can use collection.update() to update an existing document. Here is some common use cases :
Before proceed on this tutorial, you should understand how-to-do, update in mongoDB, list of supported update modifiers, and the Java DBCollection update() method.
1. Test Data
Assume following data / documents are saved into a collection.
{ "hosting" : "hostA", "type" : "vps", "clients" : 1000 }, { "hosting" : "hostB", "type" : "dedicated server", "clients" : 100 }, { "hosting" : "hostC", "type" : "vps", "clients" : 900 }
{ "_id" : { "$oid" : "x"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000} { "_id" : { "$oid" : "x"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100} { "_id" : { "$oid" : "x"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}
2. DBCollection.update() examples
See code snippets to update above saved document, with or without the modifiers.
Example 1
A normal way to update an existing document. Find hosting = hostB, and update it with a new document.
BasicDBObject newDocument = new BasicDBObject(); newDocument.put("hosting", "hostB"); newDocument.put("type", "shared host"); newDocument.put("clients", 111); collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument);
Output
{ "_id" : { "$oid" : "x"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000} { "_id" : { "$oid" : "x"} , "hosting" : "hostB" , "type" : "shared host" , "clients" : 111} { "_id" : { "$oid" : "x"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}
Example 2
This example show the use of “$inc” modifier to increase a particular value. Find hosting = hostB, update the “clients” value by increasing its value from 100 to 199, (100 + 99) = 199.
BasicDBObject newDocument = new BasicDBObject().append("$inc", new BasicDBObject().append("total clients", 99)); collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument);
Output
{ "_id" : { "$oid" : "x"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000} { "_id" : { "$oid" : "x"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 199} { "_id" : { "$oid" : "x"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}
Example 3
This example show the use of “$set” modifier to update a particular value. Find hosting = hostA, update the “type” from “vps” to “dedicated server”.
BasicDBObject newDocument3 = new BasicDBObject().append("$set", new BasicDBObject().append("type", "dedicated server")); collection.update(new BasicDBObject().append("hosting", "hostA"), newDocument3);
Output
{ "_id" : { "$oid" : "x"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100} { "_id" : { "$oid" : "x"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900} { "_id" : { "$oid" : "x"} , "hosting" : "hostA" , "clients" : 1000 , "type" : "dedicated server"}
By default, if you update a document like this :
BasicDBObject newDocument3 = new BasicDBObject().append("type", "dedicated server"); collection.update(new BasicDBObject().append("hosting", "hostA"), newDocument3);
It will replaced the entire old document, see following result :
{"_id":{ "$oid" : "x"} , "type" : "dedicated server"} {"_id":{ "$oid" : "x"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100} {"_id":{ "$oid" : "x"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}
With “$set” modifier, only particular value is updated.
Example 4
This example show the use of “multi” parameter to update a set of matched documents. Find type = “vps”, update all the matched documents, “clients” value to 888.
//find type = vps , update all matched documents , "clients" value to 888 BasicDBObject updateQuery = new BasicDBObject().append("$set", new BasicDBObject().append("clients", "888")); //both methods are doing the same thing. //collection.updateMulti(new BasicDBObject().append("type", "vps"), updateQuery); collection.update(new BasicDBObject().append("type", "vps"), updateQuery, false, true);
Output
{ "_id" : { "$oid" : "x"} , "hosting" : "hostA" , "clients" : "888" , "type" : "vps"} { "_id" : { "$oid" : "x"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100} { "_id" : { "$oid" : "x"} , "hosting" : "hostC" , "clients" : "888" , "type" : "vps"}
If you update a document without the “multi” parameter like this :
BasicDBObject updateQuery = new BasicDBObject().append("$set", new BasicDBObject().append("total clients", "888")); //multi default to false collection.update(new BasicDBObject().append("type", "vps"), updateQuery);
You will noticed that only the first matched document is updated.
{"_id":{ "$oid" : "x"} , "hosting" : "hostA" , "clients" : "888" , "type" : "vps"} {"_id":{ "$oid" : "x"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100} {"_id":{ "$oid" : "x"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}
To update a set of matched documents, you need to set “multi” to true.
3. Full Example
Full example by combining above code snippets.
package com.mkyong.core; 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; /** * Java MongoDB : Update document * */ public class UpdateDocumentApp { 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("hosting", "hostA"); document.put("type", "vps"); document.put("clients", 1000); BasicDBObject document2 = new BasicDBObject(); document2.put("hosting", "hostB"); document2.put("type", "dedicated server"); document2.put("clients", 100); BasicDBObject document3 = new BasicDBObject(); document3.put("hosting", "hostC"); document3.put("type", "vps"); document3.put("clients", 900); 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("yourdb"); // get a single collection DBCollection collection = db.getCollection("dummyColl"); System.out.println("Testing 1..."); insertDummyDocuments(collection); //find hosting = hostB, and update it with new document BasicDBObject newDocument = new BasicDBObject(); newDocument.put("hosting", "hostB"); newDocument.put("type", "shared host"); newDocument.put("clients", 111); collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("Testing 2..."); insertDummyDocuments(collection); //find hosting = hostB and increase its "clients" value by 99 BasicDBObject newDocument2 = new BasicDBObject().append("$inc", new BasicDBObject().append("clients", 99)); collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument2); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("Testing 3..."); insertDummyDocuments(collection); //find hosting = hostA and update type to from "vps" to "dedicated server" BasicDBObject newDocument3 = new BasicDBObject().append("$set", new BasicDBObject().append("type", "dedicated server")); collection.update(new BasicDBObject().append("hosting", "hostA"), newDocument3); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("Testing 4..."); insertDummyDocuments(collection); //find type = vps , update all matched documents , clients value to 888 BasicDBObject updateQuery = new BasicDBObject().append("$set", new BasicDBObject().append("clients", "888")); //both method are same //collection.updateMulti(new BasicDBObject().append("type", "vps"), updateQuery); collection.update( new BasicDBObject().append("type", "vps"), updateQuery, false, true); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("Done"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } } }
References
- How to do updating in MongoDB
- Update modifiers operations in MongoDB
- Java MongoDB APIs , DBCollection.update()






Hi,
What is the usage of ‘total’ in append(“total clients”, 99)? Is it a typo error?
[...] Java MongoDB update example/ [...]