Welcome to a tutorial on Java Integration with MongoDB. Here you will learn how to integrate MongoDB with Java, and you will also explore the basic CRUD (Create, Retrieve, Update and Delete) operations.
The first thing to do is to Download Jar and use mongo-java-driver-3.10.0.jar. Then add the jar to the Java build path. At this point, you are ready to perform CRUD operations with MongoDB through Java.
For this tutorial, we will be using MongoDB Compass. A GUI tool for MongoDB to visualize our query output.
We can make use of this user interface to connect to the DB and test our queries here.
After we must have added the mongo-java-driver-3.10.0.jar file as a dependency in our Java project. then we will be adding Maven dependency as shown below.
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.10.0</version>
</dependency>
Use this link to find the latest version of the above Mongo Java driver.
In the example below we have a simple Java class with a main() method that makes use of the MongoClient class to set up the connection with MongoDB.
package com.mongo;
import com.mongodb.DB;
import com.mongodb.MongoClient;
public class MakeConnection {
public static void main(String[] args) {
try {
// code to create the connection
MongoClient mongoClient = new MongoClient("localhost", 27017);
// code to connect to the database
DB db = mongoClient.getDB("DemoDB");
System.out.println("Database DemoDB connected successfully");
}
catch(Exception e) {
e.printStackTrace();
}
}
}
If we execute the code above, the output below will be produced on our console.
Take note that if you are using an older version of the mongodb java driver which is <= 2.10.0, then you should use the code highlighted below with Mongo class to create the connection.
// code to create the connection
Mongo mongo = new Mongo("localhost", 27017);
// code to connect to the database
DB db = mongoClient.getDB("DemoDB");
Now, if your MongoDB is running in authenticated mode, there is a need to provide the username and password while connecting to the database, as shown below.
// code to connect to the database with authentication
DB db = mongoClient.getDB("DemoDB");
boolean auth = db.authenticate("username", "password".toCharArray());
Now, as we are able to establish a connection successfully by using MongoClient, the code below can be used to display all available databases.
// display all the databases
mongoClient.getDatabaseNames().forEach(System.out::println);
The output of the code:
local 0.000GB
DemoDB 0.000GB
The above java code used to display a list of all the databases is similar to the MySQL command i.e. show databases
Now if, we create a collection (that is something like a table in relational DB) from our java program. This is shown below.
package com.mongo;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.BasicDBObject;
public class MakeConnection {
public static void main(String[] args) {
try {
// code to create the connection
MongoClient mongoClient = new MongoClient("localhost", 27017);
// code to connect to the database
DB db = mongoClient.getDB("DemoDB");
db.createCollection("User", new BasicDBObject());
System.out.println("Collection User created successfully");
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Now, upon execution of the above code, we will have a new collection with the name User which will be created in our database.
Also, we can see the collections in the MongoDB Compass UI, as shown below.