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¶
![]() | 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 Version | MongoDB 4.4 | MongoDB 4.2 | MongoDB 4.0 | MongoDB 3.6 | MongoDB 3.4 | MongoDB 3.2 | MongoDB 3.0 | MongoDB 2.6 |
---|---|---|---|---|---|---|---|---|
Version 4.2 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
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 | ✓ [1] | ✓ | ✓ | |||||
Version 2.13 | ✓ | ✓ | ||||||
Version 2.12 | ✓ |
[1] | 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. |
The driver does not support older versions of MongoDB.
Language Compatibility¶
Java Driver Version | Java 11 [2] | Java 8 | Java 7 | Java 6 | Java 5 |
---|---|---|---|---|---|
Version 4.2 | ✓ | ✓ | |||
Version 4.1 | ✓ | ✓ | |||
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 | ✓ | ✓ | ✓ | ✓ | ✓ |
[2] | Java versions 8 and above are all supported thanks to the JVM backwards compatibility promise. Only LTS versions will be explicitly listed in future. |
For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables.
How to get help¶
- Ask questions on our MongoDB Community Forums.
- Visit our Support Channels.
- See Issues & Help.
Give Feedback