Increment Value Of field in Document In Collection In MongoDb in java

MongoDB java tutorial


Contents of page >
  • Program to Increment Value Of field in Document In Collection In MongoDb in java >


Program to Increment Value Of field in Document In Collection In MongoDb in java >
package updateDocumentInCollectionInMongoDB;
/**
* Write a program to Increment Value Of field in Document In Collection In MongoDb in java
*/
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.WriteResult;
//Increment and Decrement Value In MongoDb
public class IncrementValueOfDocumentInCollectionInMongoDb {
   public static void main(String[] args) {
          /* Connect to MongoDB */
          MongoClient mongoClient =
                       new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
         
          /* Connect to DATABASE */
          //If in case database doesn't exists, it will be created at runtime
          DB db = mongoClient.getDB("mydb");
                      
          /* Get the COLLECTION (TABLE) */
          //If in case collection (Table) doesn't exists, it will be created at runtime
          DBCollection dbCollection = db.getCollection("Employee");
          /*If collection already exists, you may remove everything
          from collection for perfect OUTPUT of program*/
          //dbCollection.remove(new BasicDBObject()); //Not Mandatory
                      
         
          /* create a BasicDBObject (document /record) */
          BasicDBObject basicDBObject1 = new BasicDBObject();
          basicDBObject1.put("id", 1);
          basicDBObject1.put("salary", 100);
         
          /* Insert document in collection in MongoDB */
          System.out.println("Insert document1");
          dbCollection.insert(basicDBObject1);
         
          /* Display documents of collection before INCREMENT in MongoDB */
          System.out.println("\nDisplay documents of collection before INCREMENT in MongoDB");
          DBCursor cursor0 = dbCollection.find();
          while (cursor0.hasNext()) {
                 System.out.println(cursor0.next());
          }
         
          /** At this stage Employee collection looks like this >
               { "_id" : { "$oid" : "58555eb5ad8dc82c98cf958b"} , "id" : 1 , "salary" : 100}
          */
         
         
          /** Now, let's UPDATE the document to INCREMENT salary */
         
          /** Search for document whose id = 1 */
          BasicDBObject basicDBObject_SearchCondition = new BasicDBObject();
          basicDBObject_SearchCondition.put("id", 1);
          /** INCREMENT salary by 22 where id = 1 */
          BasicDBObject basicDBObject_UpdatedValue = new BasicDBObject();
          //Pass > $inc as key > and >
           //         BasicDBObject object with salary to be increment as value
          basicDBObject_UpdatedValue.put("$inc",
                                     new BasicDBObject().append("salary", 22));
          /** Execute UPDATE query to INCREMENT salary */
          WriteResult updateResult =
                       dbCollection.update(basicDBObject_SearchCondition,
                                                      basicDBObject_UpdatedValue);
          System.out.println("\nExecute UPDATE query to INCREMENT salary where id =1");
          System.out.println("No of documents updated = " + updateResult.getN());
          /* Display documents of collection after INCREMENT in MongoDB */
          System.out.println("\nDisplay documents of collection after INCREMENT in MongoDB");
          DBCursor cursor = dbCollection.find();
          while (cursor.hasNext()) {
                 System.out.println(cursor.next());
          }
         
          /** At this stage Employee collection looks like this >
               { "_id" : { "$oid" : "58555eb5ad8dc82c98cf958b"} , "id" : 1 , "salary" : 122}
          */
  
   }
}
/* OUTPUT
Insert document1
Display documents of collection before INCREMENT in MongoDB
{ "_id" : { "$oid" : "58555eb5ad8dc82c98cf958b"} , "id" : 1 , "salary" : 100}
Execute UPDATE query to INCREMENT salary where id =1
No of documents updated = 1
Display documents of collection after INCREMENT in MongoDB
{ "_id" : { "$oid" : "58555eb5ad8dc82c98cf958b"} , "id" : 1 , "salary" : 122}
*/

Download >
Now, it’s time to Download maven Project :) Click here.

Read : How to Import Maven project in eclipse

OR



Summary -
So in this MongoDB tutorial we learned with example and program how to to Increment Value Of field in Document In Collection In MongoDb in java.

Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.

RELATED LINKS>






eEdit
Must read for you :