Thursday, June 7, 2012

Java MongoDB Tutorial

Working with MongoDB with java involves few steps
Step 1: include the java driver into the CLASSPATH
Step 2: start the mongodb service if installed
here is the demo code

package com.test;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class MongoHelloJava {

 public static void main(String[] args) {
  try {
   //Demonstrate simple hello java program with mongoDB
   
   System.out.println("Demonstarting simple insert and retrivel in MongoDB");
   // connect to mongoDB on default port and IP address
   Mongo mongo = new Mongo("localhost",27017);
   
   // getDB() return the database from MongoDB,
      //  mongoDB will create database automatically if not existed
   DB db = mongo.getDB("myMongoDB");
   
   // getCollection() returns the collection from MongoDB
   // mongoDB will create collection automatically if not existed
   DBCollection collection = db.getCollection("myCollection");
   
   // create a document to store key and value
   BasicDBObject document = new BasicDBObject();
   document.put("id", 1000);
   document.put("print", "hello  Java");
   
   // save it into collection named "myCollection"
   collection.insert(document);
   
   // query for searching perticular id
   BasicDBObject query = new BasicDBObject();
   query.put("id", 1000);
   
   // fire query
   DBCursor cursor = collection.find(query);
   
   // iterate through cursor
   while(cursor.hasNext()){
    System.out.println(cursor.next());
   }
   
   // Demonstrate getCollection() and getCollections()
   System.out.println("Demonstrate getCollection() and getCollections()");
   Set<String> collections = db.getCollectionNames();
    

   for (String collectionName : collections) {
   System.out.println(collectionName);
   }


   //get a particular collection
     DBCollection collection1 = db.getCollection("myCollection");
   System.out.println("Collection Name is : "+collection1.toString());
   
   //insert the data into the database
   BasicDBObject document1 = new BasicDBObject();
   document1.put("post", "rajkrrsingh.blogspot.com");
   document1.put("author", "Rajkumar Singh");
   
   BasicDBObject comments = new BasicDBObject();
   comments.put("user1", "text1");
   comments.put("user2", "text2");
   comments.put("user3", "text3");

   document.put("comment", comments);
   collection.insert(document1);
   System.out.println("document1 inserted succesfully");
   
   //insertion of the data into the database through map
   Map<String, Object> documentMap = new HashMap<String, Object>();
   documentMap.put("post", "rajkrrsingh.blogspot.com");
   documentMap.put("author", "Rajkumar Singh");
    
   Map<String, Object> commentMap = new HashMap<String, Object>();
   


   commentMap.put("user1", "text1");
   commentMap.put("user2", "text2");
   commentMap.put("user3", "text3");
    
   documentMap.put("comment", commentMap);
    
   collection.insert(new BasicDBObject(documentMap));
   
   DBCursor cursor1 = collection.find();
   while (cursor1.hasNext()) {
    System.out.println(cursor1.next());
   }
   
  } catch (UnknownHostException e) {
   e.printStackTrace();
  } catch (MongoException e) {
   e.printStackTrace();
  }

 }

}



Result




Post a Comment