Navigation

Java MongoDB Driver

Introduction

The official MongoDB Java Driver providing both synchronous and asynchronous interaction with MongoDB.

Take the free online course taught by MongoDB

/figures/M220J_hero.jpg

M220J: MongoDB for Java Developers

Learn the essentials of Java application development with MongoDB.

Installation

The recommended way to get started using one of the drivers in your project is with a dependency management system. See the Installation Guide for more information.

Connect to MongoDB Atlas

To connect to a MongoDB Atlas cluster, use the Atlas connection string for your cluster:

import com.mongodb.ConnectionString;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoClientSettings;

// ...
ConnectionString connString = new ConnectionString(
    "mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority"
);
MongoClientSettings settings = MongoClientSettings.builder()
    .applyConnectionString(connString)
    .retryWrites(true)
    .build();
MongoClient mongoClient = MongoClients.create(settings);
MongoDatabase database = mongoClient.getDatabase("test");

See Connect to MongoDB for more ways to connect.

Compatibility

MongoDB Compatibility

Java Driver VersionMongoDB 4.4MongoDB 4.2MongoDB 4.0MongoDB 3.6MongoDB 3.4MongoDB 3.2MongoDB 3.0MongoDB 2.6
Version 4.1
Version 4.0
Version 3.12
Version 3.11
Version 3.10
Version 3.9
Version 3.8
Version 3.7
Version 3.6
Version 3.5
Version 3.4
Version 3.3
Version 3.2
Version 3.1
Version 3.0
Version 2.14 []
Version 2.13
Version 2.12

The driver does not support older versions of MongoDB.

[]() The 2.14 driver does not support all MongoDB 3.2 features (e.g., read concern); however, if you are currently on a version 2.x driver and would like to run against MongoDB 3.2 but cannot upgrade to driver version 3.2, use the 2.14 driver.

Language Compatibility

Java Driver VersionJava 5Java 6Java 7Java 8Java 11 []
Version 4.0
Version 3.12
Version 3.11
Version 3.10
Version 3.9
Version 3.8
Version 3.4
Version 3.3
Version 3.2
Version 3.1
Version 3.0
Versions >= 2.7
[]() Java versions 8 and above are all supported thanks to the JVM backwards compatibility promise. Only LTS versions will be explicitly listed in future.

How to get help