<p>In this tutorial we just describe <b>Hello World Example of MongoDB</b> with Java Language. Simple example to create database and create collection, inserting , deleting and updating data into document in the Mongo database.</p>
<p>Follow the following steps for this application.</p>
<p><b>Step 1: Downloading the <a href="http://www.mongodb.org/downloads" target="_blank" rel="noopener">MongoDB 2.2.2 from official site</a>. Unzip the file set to particular directory c:/mongodb/.</b></p>
<p><b>Step 2: Downloading the <a href="http://github.com/mongodb/mongo-java-driver/downloads" target="_blank" rel="noopener">MongoDB Java Driver from the official site</a>.</b></p>
<ul>
<li>mongo-java-driver-2.7.3</li>
</ul>
<p><b>Step 3: </b>Creating following directory structure &#8220;<b>MongoDBJavaHelloWorld</b>&#8221; and <b>com.dineshonjava.mongo.test</b> package and <b>libs </b>folder.<br />
<img src="https://dineshonjava.com/wp-content/uploads/2013/01/mongodirectory.png" alt="Java" border="0" /></p>
<p><b>Step 4: Creating <i>HelloWorldExample.java</i> file. A Java program to work with <i>MongoDB</i>. See comments for explanation.</b></p>
<pre class="highlight">package com.dineshonjava.mongo.test; 
 
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; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class HelloWorldExample { 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) { 
 try { 
 // connect to mongoDB, IP and port number 
 Mongo mongo = new Mongo("localhost", 27017); 
 
 // get database from MongoDB, 
 // if database doesn't exists, mongoDB will create it automatically 
 DB db = mongo.getDB("dineshonjavaDB"); 
 
 // Get collection from MongoDB, database named "dineshonjavaDB" 
 // if collection doesn't exists, mongoDB will create it automatically 
 DBCollection collection = db.getCollection("dineshonjavaCollection"); 
 
 // create a document to store key and value 
 BasicDBObject document = new BasicDBObject(); 
 document.put("id", 1000); 
 document.put("msg", "Hello World mongoDB in Java! Dinesh On Java"); 
 
 // save it into collection named "dineshonjavaCollection" 
 collection.insert(document); 
 
 // search query 
 BasicDBObject searchQuery = new BasicDBObject(); 
 searchQuery.put("id", 1000); 
 
 // query it 
 DBCursor cursor = collection.find(searchQuery); 
 
 // loop over the cursor and display the retrieved result 
 while (cursor.hasNext()) { 
 System.out.println(cursor.next()); 
 } 
 
 System.out.println("Done"); 
 
 } catch (UnknownHostException e) { 
 e.printStackTrace(); 
 } catch (MongoException e) { 
 e.printStackTrace(); 
 } 
 
 } 
 
} 
</pre>
<div id="ads-id" align="center"></div>
<p><b>Step 5:If every thing is fine then run as Java Application and get the following output.</b></p>
<div style="background-color: #ff99cc;">
<p><b>Output: </b><br />
{ &#8220;_id&#8221; : { &#8220;$oid&#8221; : &#8220;5104060a37ded1eaa9a85750&#8221;} , &#8220;id&#8221; : 1000 , &#8220;msg&#8221; : &#8220;Hello World mongoDB in Java! Dinesh On Java&#8221;}<br />
Done</p>
</div>
<p> ;</p>
<h4>Reference</h4>
<ol>
<li><b><a href="http://www.mongodb.org/display/DOCS/Java+Tutorial" target="_blank" rel="noopener">Java tutorial – MongoDB</a></b></li>
</ol>
<p><b>Download Source Code + Libs</b><br />
<b><a href="https://sites.google.com/site/dinesh9582486434/my-forms/MongoDBJavaHelloWorld.zip?attredirects=0&;d=1" target="_blank" rel="noopener">MongoDBJavaHelloWorld.zip</a> </b></p>
<div style="background-color: #ff99cc;"> <b> <;<;<a href="https://dineshonjava.com/create-database-or-collection-in-mongodb/">previous</a><;<; || <a href="https://dineshonjava.com/introduction-to-mongodb/">index </a>|| >;>;<a href="https://dineshonjava.com/java-mongodb-get-collection-from/">next</a>>;>;</b></div>
<p> ;</p>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/create-database-or-collection-in-mongodb/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/java-mongodb-get-collection-from/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '593'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…