Program to Create Connection To MongoDb In Java




Contents of page >
  • Program to Create Connection To MongoDb In Java


Program to Create Connection To MongoDb In Java
package createConnectionInMongoDB;
/**
* Write a program to Create Connection To 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;
public class CreateConnectionToMongoDbInJava {
   public static void main(String[] args) {
          /** Step-1 > Connect to MongoDB */
          System.out.println("Step-1 > Connect to MongoDB");
          MongoClient mongoClient =
                     new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
         
          /** Step-2 > Connect to DATABASE */
          //If in case database doesn't exists, it will be created at runtime
          System.out.println("Step-2 > Connect to DATABASE");
          DB db = mongoClient.getDB("mydb");
                      
          /* Step-3 > Get the COLLECTION (TABLE) */
          //If in case collection (Table) doesn't exists, it will be created at runtime
          System.out.println("Step-3 > Get the COLLECTION (TABLE)");
          DBCollection dbCollection = db.getCollection("employee");
          /* Step-4 > Not Mandatory
          If collection already exists, you may remove everything
          from collection for perfect OUTPUT of program*/
          System.out.println("Step-4 > Not Mandatory");
          dbCollection.remove(new BasicDBObject());
                      
          /** Step- 5 and 6 - INSERT > */
          System.out.println("Step- 5 and 6 - INSERT >");
         
          /** Step-5.1 > create a BasicDBObject1 (document /record) */
          BasicDBObject basicDBObject1 = new BasicDBObject();
          basicDBObject1.put("id", 1);  //Id is the column name,  1 is the value of column
          basicDBObject1.put("name", "Ankit");
         
          /** Step-5.2 > INSERT document1/record1 in COLLECTION in MongoDB */
          System.out.println("Step-5.2 > INSERT document1/record1 in COLLECTION in MongoDB");
          dbCollection.insert(basicDBObject1);
          /** Step-6.1 > create a BasicDBObject2 (document /record) */
          BasicDBObject basicDBObject2 = new BasicDBObject();
          basicDBObject2.put("id", 2);  //Id is the column name,  2 is the value of column
          basicDBObject2.put("name", "Sam");
         
          /** Step-6.2 > INSERT document2/record2 in COLLECTION in MongoDB */
          System.out.println("Step-6.2 > INSERT document1/record1 in COLLECTION in MongoDB");
          dbCollection.insert(basicDBObject2);
          /* Step-7 > Display documents of COLLECTION in MongoDB */
          System.out.println("Step-7 > Display documents of COLLECTION in MongoDB");
          DBCursor dbCursor = dbCollection.find();
          while (dbCursor.hasNext()) {
                 System.out.println(dbCursor.next());
          }
         
         
   }
}
/*OUTPUT
Step-1 > Connect to MongoDB
Step-2 > Connect to DATABASE
Step-3 > Get the COLLECTION (TABLE)
Step-4 > Not Mandatory
Step- 5 and 6 - INSERT >
Step-5.2 > INSERT document1/record1 in COLLECTION in MongoDB
Step-6.2 > INSERT document1/record1 in COLLECTION in MongoDB
Step-7 > Display documents of COLLECTION in MongoDB
{ "_id" : { "$oid" : "585c06a87dbba63be8c8e231"} , "id" : 1 , "name" : "Ankit"}
{ "_id" : { "$oid" : "585c06a97dbba63be8c8e232"} , "id" : 2 , "name" : "Sam"}
*/


Summary -
So in this MongoDB tutorial we learned with example and program how to Create Connection To 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 :