MongoDB: Basics with java

Now in this post we’ll see how to create database, collection, accessing documents from collection, and other CRUD operation and useful function using Java.


For demo you need to add Java driver for MongoDB in your classpath.

You can download a driver from official MongoDB site.

For all demos in we are using mongo-2.10.1.jar.

Connect to server

To connect with Mongo Server you can use either MongoClient or Mongo object.

Here we are using MongoClient.
Java Code:

For basics you have to give a port on which server is listening and server ip/computer name or localhost if server is in local machine.

Creating Database


here getDB(String dbName) method returns object of class DB.

In this case if database “kode12” already exist than it’ll return as it is. Otherwise create an empty Database with same name and return.

Java Code:


Creating Collection and Insert Document

Using java you have to get DBCollection object using object of class DB like,

This will return object of class DBCollection which is point to give collection of given database, now you can do all operation on collection.

MongoDB Command

In shell we write String to insert in { … }, but in java you have to create BasicDBObject instance.

BasicDBObject behave likeMap, it store data in Key Value pair.

There are many way to create BasicDBObject like you also can create BasicDBObject byMap. you can refer API for more.

Now you can use save(DBObject dbObject) or insert(DBObject dbObject) method to insert given object in database.The difference between save(…) and insert(..) is what we noticed is save has limited no of function while insert has many choice to insert data like insert(List), insert(DBObject[], WriteConcern) etc… .

Note: save and insert method’s signature is DBObject so don’t get confused, DBObject is Interface and BasicDBObjectis Implementing Class of DBObject.

Java Code:

Retrieving Document

MongoDB method : find()

MySQL equivalent : SELECT * FROM post;
To retrieve using java again you have to Create object of implementing class of interface DBObject (for ex, BasicDBObject) or implement DBObject interface using its all implementing class.

When you use find() method than its return object of DBCursor class. Which is similar like List<?> so can iterate and get all returned DBObject. Refer below code.

This’ll return all document stored in collection post.

MongoDB method: findOne()

MySQL equivalent: SELECT * FROM post LIMIT 1;

In Java findOne() return only one Document so you can direct get into DBObject. Refer code.

Conditional Retrieving

You can use condition while retrieving documents. Suppose I want to get all post whose author is vishal.

MySQL equivalent: SELECT * FROM post where author=’vishal’;

MongoDB equivalent:{“author”: “vishal”});

Java Code:

Updating Document

MySQL equivalent: UPDATE post set author=’yogeshm’ WHERE title=’ 2;

MongoDB equivalent:{“title”: “2. MongoDB Basics with Shell”}, {$set: {“author”: “yogeshm”}})

Java Code:

Here we create 2 instance of BasicDBObject, one is for searching for given condition and another is with new value which needs to be update.

And used update(DBObject, DBObject) method of collection.

Removing Document

MySQL equivalent: DELETE FROM post WHERE author=’vishal’;

MongoDB equivalent:{“author”: “vishal”});

Java Code:

MongoDB equivalent: db.dropDatabase();

Java Code: