Java MongoDB Connection Example

This tutorial shows how to write a Java program to connect to a standalone MongoDB server.
MongoDB is a cross-platform, document-oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on the concept of collection and document.
Learn MongoDB with Java at https://www.javaguides.net/p/java-mongodb-tutorial.html

Tools and Technologies Used

  • Java (JDK) 10
  • Maven 3.5+
  • Eclipse Neon
  • MongoDB 3.12.0

Installing MongoDB

Use the following article to install MongoDB on Windows 10.
Make sure that you have installed MongoDB and started MongoDB server on default port 27017.

Create Database

Create a new database by using the below command on the MongoDB client terminal:
> use javaguides;
switched to db javaguides
The use command will create a new database if it doesn't exist, otherwise, it will return the existing database.

Java MongoDB Driver

Let's use the following Maven declaration to include the MongoDB Java Driver in our maven project.
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
 <groupId>org.mongodb</groupId>
 <artifactId>mongo-java-driver</artifactId>
 <version>3.12.0</version>
</dependency>
Note that it is an all-in-one JAR, which embeds the core driver and BSON. BSON, short for Bin­ary JSON, is a bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments.

Java MongoDB Connection Example

This example connects to the "javaguides" database and retrieves all its collections.
package net.javaguides.mongodb.database;

import com.mongodb.client.MongoClients;

/**
 * Java MongoDB Connection Example
 * @author Ramesh Fadatare
 *
 */
public class ConnectToDB {
    public static void main(String args[]) {

        try (var mongoClient = MongoClients.create("mongodb://localhost:27017")) {

            var database = mongoClient.getDatabase("javaguides");

            System.out.println("database name -> " + database.getName());

            for (String name: database.listCollectionNames()) {

                System.out.println(name);
            }
        }
    }
}
Output:
database name -> javaguides
users
Let's understand the above example.
A MongoClient class is used to connect to the MongoDB server. It is created with the MongoClients.create() method call. The 27017 is the default port on which the MongoDB server listens.
try (var mongoClient = MongoClients.create("mongodb://localhost:27017")) {
The following code gets the database and lists its collections:
            var database = mongoClient.getDatabase("javaguides");

            System.out.println("database name -> " + database.getName());

            for (String name: database.listCollectionNames()) {

                System.out.println(name);
            }

Java MongoDB Tutorials

Learn MongoDB with Java at https://www.javaguides.net/p/java-mongodb-tutorial.html

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments