A simple Java + Maven + MongoDB hello world example.
In this Post we will see step by step how to connect MongoDB, create database, collection and document, save, update, remove, get and display document.
Configured mongo java driver dependency in below pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mahesh.common</groupId>
<artifactId>MongoDBExample</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>MongoDBExample</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- mongodb java driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<build>
<finalName>MongoDBExample</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.mahesh.core;
import com.mongodb.*;
import java.net.UnknownHostException;
import java.util.Date;
public class HelloWorldExample{
public static void main(String[] args) {
try {
/**** Connect to MongoDB ****/
// Since 2.10.0, uses MongoClient
MongoClient mongo = new MongoClient("localhost", 27017);
/**** Get database ****/
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("mahesh");
/**** Get collection / table from 'user' ****/
// if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("user");
/**** Insert ****/
// create a document to store key and value
BasicDBObject document = new BasicDBObject();
document.put("name", "mahesh");
document.put("age", 30);
document.put("createdDate", new Date());
table.insert(document);
/**** Find and display ****/
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("name", "mahesh");
DBCursor cursor = table.find(searchQuery);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
/**** Update ****/
// search document where name="mahesh" and update it with new values
BasicDBObject query = new BasicDBObject();
query.put("name", "mahesh");
BasicDBObject newDocument = new BasicDBObject();
newDocument.put("name", "mahesh-updated");
BasicDBObject updateObj = new BasicDBObject();
updateObj.put("$set", newDocument);
table.update(query, updateObj);
/**** Find and display ****/
BasicDBObject searchQuery2
= new BasicDBObject().append("name", "mahesh-updated");
DBCursor cursor2 = table.find(searchQuery2);
while (cursor2.hasNext()) {
System.out.println(cursor2.next());
}
/**** Done ****/
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}
In this Post we will see step by step how to connect MongoDB, create database, collection and document, save, update, remove, get and display document.
Below is the Pom.xml with dependencies:
Configured mongo java driver dependency in below pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mahesh.common</groupId>
<artifactId>MongoDBExample</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>MongoDBExample</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- mongodb java driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<build>
<finalName>MongoDBExample</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Below is the Content for HelloWorldExample.java
package com.mahesh.core;
import com.mongodb.*;
import java.net.UnknownHostException;
import java.util.Date;
public class HelloWorldExample{
public static void main(String[] args) {
try {
/**** Connect to MongoDB ****/
// Since 2.10.0, uses MongoClient
MongoClient mongo = new MongoClient("localhost", 27017);
/**** Get database ****/
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("mahesh");
/**** Get collection / table from 'user' ****/
// if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("user");
/**** Insert ****/
// create a document to store key and value
BasicDBObject document = new BasicDBObject();
document.put("name", "mahesh");
document.put("age", 30);
document.put("createdDate", new Date());
table.insert(document);
/**** Find and display ****/
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("name", "mahesh");
DBCursor cursor = table.find(searchQuery);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
/**** Update ****/
// search document where name="mahesh" and update it with new values
BasicDBObject query = new BasicDBObject();
query.put("name", "mahesh");
BasicDBObject newDocument = new BasicDBObject();
newDocument.put("name", "mahesh-updated");
BasicDBObject updateObj = new BasicDBObject();
updateObj.put("$set", newDocument);
table.update(query, updateObj);
/**** Find and display ****/
BasicDBObject searchQuery2
= new BasicDBObject().append("name", "mahesh-updated");
DBCursor cursor2 = table.find(searchQuery2);
while (cursor2.hasNext()) {
System.out.println(cursor2.next());
}
/**** Done ****/
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}
OutPut:
{ "_id" : { "$oid" : "53ff48a01da732e00203e04b"} , "name" : "mahesh" , "age" : 30 , "createdDate" : { "$date" : "2014-08-28T15:20:00.863Z"}}
{ "_id" : { "$oid" : "53ff47d91da779bbc8be2594"} , "age" : 30 , "createdDate" : { "$date" : "2014-08-28T15:16:41.184Z"} , "name" : "mahesh-updated"}
{ "_id" : { "$oid" : "53ff48a01da732e00203e04b"} , "age" : 30 , "createdDate" : { "$date" : "2014-08-28T15:20:00.863Z"} , "name" : "mahesh-updated"}
Done
No comments:
Post a Comment