Java Integration in MongoDB

In this, we will learn how to integrate MongoDB with Java and will explore the basic CRUD (Create, Retrieve, Update and Delete) operations.

Download Jar, use mongo-java-driver-2.10.1.jar. Add jar to the Java build path. Now, we are ready for performing CRUD operations with MongoDB through Java.

Here, we are making use of MongoDB Compass – a GUI tool for MongoDB for visualizing our query output.

Java Integration in MongoDB


Make Connection with Database:

Java Integration in MongoDB


When the above mentioned code is executed, following o/p will be produced on console

Java Integration in MongoDB


In this case we haven’t used authentication for database connection. But if it is required, we can make use of method db.authenticate(username, password);


Create Collection:

Java Integration in MongoDB

When above lines of code are executed, Collection User will be created in Database. We can check our query output through MongoDB Compass.


Java Integration in MongoDB