TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Java MongoDB Example

Java MongoDB Example for beginners and professionals with examples on CRUD, insert document, query document, update document, delete document, use database, projection etc.

<< Back to JAVA

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

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.TheDeveloperBlog</groupId>
<artifactId>java-mongo-db</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<name>java-mongodb</name>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.4.2</version>
</dependency>
</dependencies>
</project>

3) Create Java File

Create a Java file and write code to create connection.

// JavaMongoDemo.java

package com.TheDeveloperBlog.java.mongo.db;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
public class JavaMongoDemo {
public static void main(String[] args){
try{
//---------- Connecting DataBase -------------------------//
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
//---------- Creating DataBase ---------------------------//
MongoDatabase db = mongoClient.getDatabase("TheDeveloperBlog");
//---------- Creating Collection -------------------------//
MongoCollection<Document> table = db.getCollection("employee");
//---------- Creating Document ---------------------------//  
Document doc = new Document("name", "Peter John");
doc.append("id",12);
//----------- Inserting Data ------------------------------//
table.insertOne(doc);
}catch(Exception e){
System.out.println(e);
}
}
}

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.

$ mongo
MongoDB Java mongodb connectivity 4

Databases

We can see available databases by using the following command.

> show dbs
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 TheDeveloperBlog 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




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf