[NoSQL] Mongodb with Java in 5 minutes


NoSQL is database management systems which concentrates on storage and retrieval of data rather than relationship between data like RDBMS. NoSQL interpreted as “Not only SQL” as the name says — all the data manipulations to the database are done using methods and not SQL queries. NoSQL is useful in cases where the data is very huge and relationship  between the data is not important. There are a lot of NoSQL databases out there [http://nosql-database.org/]. One good open source NoSQL db with a lot of different platform and different programming language support is Mongodb. Following is a basic getting started with mongodb along with Java driver.

1. download the proper mongodb for your platform : http://www.mongodb.org/downloads for me it was Windows.

2. Unzip the above download to a folder lets say “mongodb-win32-x86_64-2.2.3” [considering mongodb version 2.2.3 for Windows 64 bit]. Inside this folder there should be a bin folder with executables to start the database and many other db utilities.

3. create a data directory inside mongodb-win32-x86_64-2.2.3 so it should look like mongodb-win32-x86_64-2.2.3/data/db.

4. create a batch file inside the mongodb-win32-x86_64-2.2.3/bin directory lets say startmongo.bat and put the following line in the bat file [adjust the paths accordingly]

/path-to-mongo/mongodb-win32-x86_64-2.2.3/bin/mongod –dbpath “/path-to-mongo/mongodb-win32-x86_64-2.2.3/data/db”

5. well thats it… run the startmongo.bat. NoSQL database is up and running. mongo db generally starts up at port 27017 which can be changed with the –port switch in the above batch file. Check out more mongod switches here http://docs.mongodb.org/manual/tutorial/manage-mongodb-processes/.

6. Download the Java driver or in other words the library file here  https://github.com/mongodb/mongo-java-driver/downloads. When you add this to a netbeans project, you get a whole lot of objects and methods to access and manipulate mondodb.

7. Following is some example Java code using the above driver. It does every thing GET, INSERT, UPDATE and REMOVE.

======== MongoTest,java ===============

package test;

import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class MongoTest {

public static void main(String[] args) {

try {

Mongo mongo = new Mongo(“localhost”, 27017);
// GET DB
DB db = mongo.getDB(“testdb”);

// GET COLLECTION
DBCollection collection = db.getCollection(“collection1”);

//INSERT documents with two fields id and datacol
collection.insert(new BasicDBObject().append(“id”, “1234”).append(“datacol”, “dummy data1”));
collection.insert(new BasicDBObject().append(“id”, “1235”).append(“datacol”, “dummy data2”));
collection.insert(new BasicDBObject().append(“id”, “1236”).append(“datacol”, “dummy data3”));

// UPDATE document with id = 1234
BasicDBObject newDocument = new BasicDBObject();
newDocument.put(“id”, “1234”);
newDocument.put(“datacol”, “dummy data4”);
collection.update(new BasicDBObject().append(“id”, “1234”), newDocument);

//GET first document
DBObject doc = collection.findOne();
//DELETE first document
collection.remove(doc);

//DELETE document with id = 2
BasicDBObject document = new BasicDBObject();
document.put(“id”, “2”);
collection.remove(document);

//GET all documents in collection
DBCursor cursor = collection.find();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}

//DELETE all documents
collection.remove(new BasicDBObject());

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}

======== MongoTest,java ===============

Futher Reading:

http://en.wikipedia.org/wiki/NoSQL

http://www.mongodb.org/

http://docs.mongodb.org/ecosystem/drivers/java/

Advertisements

About Dominic

J for JAVA more about me : http://about.me/dominicdsouza
This entry was posted in Thechy Stuff. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s