Friday, 6 March 2015

mongoDB : javaAPI : inc operator


> db.employee.find()
{ "_id" : 1, "firstName" : "Jessi", "lastName" : "chelli", "designation" : "Software Engineer", "salary" : 50000 }
{ "_id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru", "salary" : 50000 }
{ "_id" : 3, "firstName" : "Gopi", "lastName" : "Battu", "salary" : 30000 }
{ "_id" : 4, "firstName" : "Ritwik", "lastName" : "Mohenthy", "salary" : 30000 }
{ "_id" : 5, "firstName" : "Ananad", "lastName" : "Bandaru", "designation" : "Project Manager", "salary" : 30000 }

Increase salary of Jessi by 5000.

BasicDBObject query = new BasicDBObject();
BasicDBObject update = new BasicDBObject();

query.put("firstName", "Jessi");
update.append("$inc", new BasicDBObject().append("salary", 5000));

collection.update(query, update);

Above snippet add 5000 to the salary of Jessi.

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", "Jessi");
    update.append("$inc", new BasicDBObject().append("salary", 5000));   
    
    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.update(query, update);
    
    System.out.println("After updating");
    
    cursor = collection.find();
    while(cursor.hasNext()){
      System.out.println(cursor.next());
    }
  }
  
}


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

Before updating
{ "_id" : 1.0 , "firstName" : "Jessi" , "lastName" : "chelli" , "designation" : "Software Engineer" , "salary" : 50000.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" : "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}

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment