Friday 6 March 2015

mongoDB : Java : Update multiple documents


By default update() method updates one document which matches the search criteria. If you want to update multiple documents that match search criteria, then use “updateMulti” function.

BasicDBObject query = new BasicDBObject();
BasicDBObject update = new BasicDBObject();
               
query.put("firstName", "Ananad");
update.append("$set", new BasicDBObject().append("firstName", "Anand"));

collection.updateMulti(query, update);

Above snippet update all documents where firstName is “Ananad” as “Anand”.

import java.net.UnknownHostException;

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

public class UpdateDocument {

  /* Step 1 : get mongoClient */
  public static MongoClient getMongoClient(){
    MongoClient mongoClient = null;
     try {
       mongoClient = new MongoClient( "localhost" , 27017 );
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
     return mongoClient;
  }
  
  public static void main(String args[]){
    MongoClient mongoClient = getMongoClient();
    
    /* Step 2: Connect to DB */
    DB db = mongoClient.getDB("sample");
    
    /*Step 3 : Get collection */
    DBCollection collection = db.getCollection("employee");
    
    /* Step 4 : Create Query and update objects */
    BasicDBObject query = new BasicDBObject();
    BasicDBObject update = new BasicDBObject();
    
    query.put("firstName", "Ananad");
    update.append("$set", new BasicDBObject().append("firstName", "Anand"));   
    
    System.out.println("Query");
    System.out.println("*******************");
    System.out.println(query + "\n");
    
    System.out.println("Before updating");
    
    DBCursor cursor = collection.find();
    while(cursor.hasNext()){
      System.out.println(cursor.next());
    }
        
    /* Step 5 : Update document */
    collection.updateMulti(query, update);
    
    System.out.println("After updating");
    
    cursor = collection.find();
    while(cursor.hasNext()){
      System.out.println(cursor.next());
    }
  }
  
}


Output
Query
*******************
{ "firstName" : "Ananad"}

Before updating
{ "_id" : 1.0 , "firstName" : "Jessi" , "lastName" : "chelli" , "designation" : "Software Engineer" , "salary" : 55000.0}
{ "_id" : 2.0 , "firstName" : "Ananad" , "lastName" : "Bandaru" , "salary" : 50000.0}
{ "_id" : 3.0 , "firstName" : "Gopi" , "lastName" : "Battu" , "salary" : 30000.0}
{ "_id" : 4.0 , "firstName" : "Ritwik" , "lastName" : "Mohenthy" , "salary" : 30000.0}
{ "_id" : 5.0 , "firstName" : "Ananad" , "lastName" : "Bandaru" , "designation" : "Project Manager" , "salary" : 30000.0}
After updating
{ "_id" : 1.0 , "firstName" : "Jessi" , "lastName" : "chelli" , "designation" : "Software Engineer" , "salary" : 55000.0}
{ "_id" : 2.0 , "firstName" : "Anand" , "lastName" : "Bandaru" , "salary" : 50000.0}
{ "_id" : 3.0 , "firstName" : "Gopi" , "lastName" : "Battu" , "salary" : 30000.0}
{ "_id" : 4.0 , "firstName" : "Ritwik" , "lastName" : "Mohenthy" , "salary" : 30000.0}
{ "_id" : 5.0 , "firstName" : "Anand" , "lastName" : "Bandaru" , "designation" : "Project Manager" , "salary" : 30000.0}





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment