Home » Java » Enterprise Java » MongoDB with Java Kickstart

About Pavithra Siriwardena

Pavithra Siriwardena

MongoDB with Java Kickstart

NoSQL databases due to their scalability are becoming increasingly popular. When used appropriately
NoSQL databases can offer real benefits. MongoDB is such a highly scalable opensource NoSQL database written in C++.

1. Installing MongoDB

Without much of a trouble you can install MongoDB using the instructions given in the official MongoDB site, according to whatever the OS you are using.

2. Starting the MongoDB server

This is quite simple. Run the mongod.exe file inside bin folder(I am using windows OS here) to start the MongoDB server.

By default the server will start on port 27017 and the data will be stored at /data/db directory which you’ll have to create during the installing process.

3. Starting MongoDB shell

You can start the MongoBD shell by running the mongo.exe file.

4. Creating a database with MongoDB

To create a database named ‘company’ using MongoDB type the following on MongoDB shell

 use company 

Mind that MangoDB will not create a database until you save something inside it.

Use following command to view the available databases and that will show you that ‘company’ database hasn’t been created yet.

 show dbs; 

5. Saving data in MongoDB

Use following commands to save employee data to a collection called employees

employee = {name : 'A', no : 1} 

To view the data inside the collection use following command,


Do it with Java :)

Following is a simple Java code which is doing the same thing we did above. You can get the mongo-java driver from here.

Just go through the code, it’s very simple, hopefully you’ll get the idea.

package com.eviac.blog.mongo;

import java.net.UnknownHostException;

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 MongoDBClient {

 public static void main(String[] args) {

  try {

   Mongo mongo = new Mongo('localhost', 27017);

   DB db = mongo.getDB('company');

   DBCollection collection = db.getCollection('employees');

   BasicDBObject employee = new BasicDBObject();
   employee.put('name', 'Hannah');
   employee.put('no', 2);


   BasicDBObject searchEmployee = new BasicDBObject();
   searchEmployee.put('no', 2);

   DBCursor cursor = collection.find(searchEmployee);

   while (cursor.hasNext()) {

   System.out.println('The Search Query has Executed!');

  } catch (UnknownHostException e) {
  } catch (MongoException e) {




{ '_id' : { '$oid' : '4fec74dc907cbe9445fd2d70'} , 'name' : 'Hannah' , 'no' : 2}
The Search Query has Executed! 

Reference: MongoDB with Java from our JCG partner Pavithra Siriwardena at the EVIAC blog.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
Email address:

Leave a Reply

Notify of