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} 
db.employees.save(employee) 

To view the data inside the collection use following command,

 
db.users.find(); 

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);

   collection.insert(employee);

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

   DBCursor cursor = collection.find(searchEmployee);

   while (cursor.hasNext()) {
    System.out.println(cursor.next());
   }

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

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

 }

}

Result

{ '_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.

Related Whitepaper:

Professional NoSQL

A hands-on guide to leveraging NoSQL databases!

NoSQL databases are an efficient and powerful tool for storing and manipulating vast quantities of data. Most NoSQL databases scale well as data grows. In addition, they are often malleable and flexible enough to accommodate semi-structured and sparse data sets. This comprehensive hands-on guide presents fundamental concepts and practical solutions for getting you ready to use NoSQL databases. Expert author Shashank Tiwari begins with a helpful introduction on the subject of NoSQL, explains its characteristics and typical uses, and looks at where it fits in the application stack. Unique insights help you choose which NoSQL solutions are best for solving your specific data storage needs.

Get it Now!  

Leave a Reply


× 9 = nine



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books