DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Do Java on MongoDB

How to Do Java on MongoDB

Pavithra Gunasekara user avatar by
Pavithra Gunasekara
·
Jul. 02, 12 · Interview
Like (0)
Save
Tweet
Share
7.08K Views

Join the DZone community and get the full member experience.

Join For Free

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!   
MongoDB Java (programming language) Database

Published at DZone with permission of Pavithra Gunasekara, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring Cloud
  • Use Golang for Data Processing With Amazon Kinesis and AWS Lambda
  • HTTP vs Messaging for Microservices Communications
  • Cucumber.js Tutorial With Examples For Selenium JavaScript

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: