Showing posts with label NoSQL. Show all posts
Showing posts with label NoSQL. Show all posts

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




Tuesday, June 5, 2012

MongoDB Tutorial : Getting Started

MongoDB is no SQL,open source database, written purely in C++, basically store the database in the document form.
based on the JSON approach,can be horizontally scalable,can be index on any attribute

Here are the best practise to use











How to download and Install MongoDB
Download the MongoDB from the MongoDB website as depicted in the picture as per your
plateform,here I am downloading it for the Windows 32 bits.











Download the relevent Driver to work for
The fallowing page shows the link for the driver,Here I am working with the Java Driver.








Review the downloaded bineries
After downloading MongoDB,extract the zip file which contains the fallowing exe files as shown in the image.








Installing the MongoDB server
Intalling the MongoDB server is not a time taking process,just open the command prompt and go the location of bin of the unzipped folder,in the meanwhile create the
database folder as MongoDB does not create the database itself.

execute the fallowing command mongod --dbpath "f://mongodatabase"
and press enter the fallowing screen show the out that your server is up and running on the default port 27017











Test your DB Installation
Open the web browser and type the fallowing addres http://loacalhost:27017/
The webpage displays the welcome page of mongoDB same as the image shown.









Connect to MongoDB
Open the command prompt, open the bin location






run the MongoDB as a Windows Service
open the command prompt and execute the fallowing command
mongod --dbpath "f:\mongodatabase" --logpath "f:\mongodatabase\logs.txt" --install --serviceName "MongoDB"
here shown the output on the console





The Next step is to verify from the service manager





Uninstall
Use the remove command shown in the image.




Create Database in the MongoDB
MongoDB does not provide any command or syntex to create database as provided with the relationship based
database,it creates the database on the fly when you try to insert the values first time in the database.

on the shell use show dbs command which shows the local(empty) database




use databasename command will create your MongoDB,but the database has not been created yet you can
verify the same by using show dbs command.
The database only created when you try to insert some document into the database
Define a collection name as employee and let try to insert some document inside it