Javatpoint Logo
Javatpoint Logo

Java MongoDB Connectivity

To connect with MongoDB database, Java project includes the following steps. Here, we are creating a maven project and providing dependency for the mongodb-driver.

Follow the Steps:

1) Create a Maven Project

MongoDB Java mongodb connectivity 1

2) Add Dependecy to POM file

// pom.xml

3) Create Java File

Create a Java file and write code to create connection.

// JavaMongoDemo.java

4) Build Dependency to load JAR

MongoDB Java mongodb connectivity 2

Project Structure

Finally, our project structure look like this.

MongoDB Java mongodb connectivity 3

5) Enter into Mongo Shell

Make sure, we have mongoDB installed. After installing, enter into mongo shell by typing the following command.

MongoDB Java mongodb connectivity 4

Databases

We can see available databases by using the following command.

MongoDB Java mongodb connectivity 5

We can see that there is no database available. let's create a database.

Run Java File

Run this file to create database.

MongoDB Java mongodb connectivity 6

Database

Check MongoDB again to see the available databases.

MongoDB Java mongodb connectivity 7

See, a database javatpoint is created.

Collection

See, the created collection employee.

MongoDB Java mongodb connectivity 8

Record

See the inserted record.

MongoDB Java mongodb connectivity 9

Well, we can see that the Java program is executing fine and we can also perform other databases operations as well.


Next TopicPHP MongoDB




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA