Update Multiple Documents MongoDB Java Example

This post will talk about Update Multiple Documents MongoDB Java Example.
In the last few examples MongoDB Official Logowe have seen that, we are using the default version of update() method , where it takes 2 arguments i.e. search criteria object and modified object. Whenever this version of update() method is executed it updates only the first document that matches the search criteria , now suppose in your application you want to update more than 1 documents matching the passed search criteria then the default version of update() method (2 arguments) won’t solve your problem.

MongoDB provides you the flexibility to update more than one documents by using the overloaded version of update() method that takes 4 arguments. One example of that we have already seen in the previous post where we have seen the upsert example by setting the upsert flag true.
Same type of flag also exists for updating multiple documents , if you enable that flag it will update all the documents matching search criteria else it will update only the first one.

We will continue with the same sample documents that we have used in our all the previous examples. You can see it here.
In our collection we have 5 documents and I want to update all the documents which belong to MongoDB founders. In both of these documents we have stored the “founder” key value as “mongo” and now I want to update it with “mongodb”.Below is the sample code for that.

MutipleDcoumentUpdate.java :

/**
 * 
 */
package com.lotusmedia.mongo;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

/**
 * @author yoMongo
 *
 */
public class MultipleDocumentUpdate {
	
	private static void updateDocuments(){
		DBCollection lotusCollection=MongoUtils.getCollection("yoMongo");
		BasicDBObject searchObject = new BasicDBObject();
		searchObject.put("founder", "mongo");
		
		DBObject modifiedObject =new BasicDBObject();
		modifiedObject.put("$set", new BasicDBObject().append("founder", "mongodb"));
		
		lotusCollection.update(searchObject, modifiedObject,false,true);
		MongoUtils.printAllDocument("founder", "mongodb");		
	}
	
	
	public static void main(String[] args) {
		MultipleDocumentUpdate.updateDocuments();
	}

}

Important thing to note here is the 4th argument of the update() method that we have passed it as true and this flag will make sure that multiple documents will be updated matching a search criteria.
When you execute the above code , it will update all the documents where founder key has a value “mongo” to the new value “mongodb”.
Output :

{ "_id" : { "$oid" : "53612c22a2f88cef8db5e9bd"} , "firstName" : "kevin" , "lastName" : "ryan" , "founder" : "mongodb" , "wiki" : "http://en.wikipedia.org/wiki/Kevin_P._Ryan" , "hits" : 32000}
{ "_id" : { "$oid" : "53612c22a2f88cef8db5e9c0"} , "firstName" : "eliot" , "lastName" : "Horowitz" , "founder" : "mongodb" , "wiki" : "http://en.wikipedia.org/wiki/Eliot_Horowitz" , "hits" : 38000}

Code for MongoUtils.java can be find here.

Image Credits : http://mongodb.org

Let'sConnect

Saurabh Jain

A Developer working on Enterprise applications ,Distributed Systems, Hadoop and BigData.This blog is about my experience working mostly on Java technologies ,NoSQL ,git , maven and Hadoop ecosystem.
Let'sConnect

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

One thought on “Update Multiple Documents MongoDB Java Example

Add Comment Register



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>