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

../../_images/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 Installation Guide

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

The following compatibility table specifies the recommended version(s) of the MongoDB Java driver for use with a specific version of MongoDB.

The first column lists the driver version(s).

Java Driver Version MongoDB 4.2 MongoDB 4.0 MongoDB 3.6 MongoDB 3.4 MongoDB 3.2 MongoDB 3.0 MongoDB 2.6
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

The following compatibility table specifies the recommended version(s) of the MongoDB Java driver for use with a specific version of Java.

The first column lists the driver version(s).

Java Driver Version Java 5 Java 6 Java 7 Java 8 Java 11 [†]
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