Thursday, August 28, 2014

How to use MongoDB with Java


t is a step-by-step guide to Integrate MongoDB with a Java Application using Eclipse. Java Application example is also provided as a demo.
                           

Step-1: Download the latest version of MongoDB here:
Step-2: Steps to Install MongoDB depends upon your Operating System and they are available here:
Step-3: Download latest Java Driver to Connect Your Java Application to MongoDB from here:
Step-4: Create a New Java Application or a Dynamic Web Project:
After creating the project, go to Project > Properties > Java Build Path > Add Variable.


Go to: Configure Variable > New > Name: "MongoDB" > Path: Java Driver for MongoDB downloaded in Step-3.



Step-5: If you using Maven Project then you should add dependencies accordingly and add into pom.xml. If you are using simple Java application
then add java driver as external library from Properties > Java Build Path > Add External Libraries and if you are using Dynamic Web Project then
add(copy) java driver to lib folder.
Step-6: Below is a Example(Java Application) of how to create MongoDB database and other important functions:

import java.net.UnknownHostException;

import java.util.Date;

import com.mongodb.BasicDBObject;

import com.mongodb.DB;

import com.mongodb.DBCollection;

import com.mongodb.DBCursor;

import com.mongodb.MongoClient;

import com.mongodb.MongoException;

// Java MongoDB Example
public class MongoEg {

public static void main(String[] args) {

try {

//Connect to MongoDB
MongoClient mongo = new MongoClient("localhost", 27017);

// Get database
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("MongoDb");

//Get collection(table) from 'MongoDb'
// if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("user");

// Insert item
// create a document to store key and value
BasicDBObject document = new BasicDBObject();

document.put("name", "Priyank");

document.put("age", 24);

document.put("organization", "ASU");

document.put("createdDate", new Date());

table.insert(document);
// Find and display

BasicDBObject searchQuery = new BasicDBObject();

searchQuery.put("name", "Priyank");

DBCursor cursor = table.find(searchQuery);

while (cursor.hasNext()) {

System.out.println(cursor.next());

 }

//Update item
BasicDBObject query = new BasicDBObject();

query.put("name", "Priyank");

BasicDBObject newDocument = new BasicDBObject();

newDocument.put("name", "Priyank-Sharma");

BasicDBObject updateObj = new BasicDBObject();

updateObj.put("$set", newDocument);

table.update(query, updateObj);

System.out.println("Done");

 } 

catch (UnknownHostException e) {

e.printStackTrace();

 } 

catch (MongoException e) {

e.printStackTrace();
    
      }

   }

 }
Step-7: Right click on the java file > Run As > Java Application: Below is the screen-shot how you will see the result in your console. You can
check it using mongo command line.







No comments:

Post a Comment