Insert JSON In Collection In MongoDb in java




Contents of page >
  • Program to Insert JSON In Collection In MongoDb in java >


Program to Insert JSON In Collection In MongoDb in java >
package insertDocumentInCollectionInMongoDB;
/**
* Write a program to Insert JSON 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.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.util.JSON;
public class InsertDocumentInCollectionInMongoDb2_Json {
   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 JSON 1 */
          String json1= "{id : 1,  firstName:'ankit'}";      
          DBObject dbObject1 = (DBObject)JSON.parse(json1);
         
          /** INSERT JSON1 in COLLECTION in MongoDB */
          System.out.println("INSERT JSON1 >");
          dbCollection.insert(dbObject1);
         
          /** Create JSON 2 */
          String json2= "{id : 2,  firstName:'sam'}";        
          DBObject dbObject2  = (DBObject)JSON.parse(json2);
          /** INSERT JSON2 in COLLECTION in MongoDB */
          System.out.println("INSERT JSON1 >");
          dbCollection.insert(dbObject2);
         
          /* Display documents of  COLLECTION in MongoDB */
          System.out.println("Display documents of COLLECTION in MongoDB >");
          DBCursor cursor = dbCollection.find();
          while (cursor.hasNext()) {
                 System.out.println(cursor.next());
          }
  
   }
}
/* OUTPUT
INSERT JSON1 >
INSERT JSON1 >
Display documents of COLLECTION in MongoDB >
{ "_id" : { "$oid" : "585c102a7dbba63084e26c1c"} , "id" : 1 , "firstName" : "ankit"}
{ "_id" : { "$oid" : "585c102a7dbba63084e26c1d"} , "id" : 2 , "firstName" : "sam"}
*/

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 Insert JSON 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 :