MongoDB Java Reactive Streams
On this page
Introduction
Welcome to the documentation site for the Reactive Streams Driver, the official MongoDB driver for asynchronous Java applications. Download it by following the Installation Guide or set up a runnable project by following our tutorials.
If you need to make synchronous calls between your application and MongoDB, use the MongoDB Java Driver instead of the Reactive Streams Driver.
Installation
The recommended way to get started using the driver 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.reactivestreams.client.MongoClients; import com.mongodb.reactivestreams.client.MongoClient; import com.mongodb.reactivestreams.client.MongoDatabase; import com.mongodb.MongoClientSettings; // ... // Replace the uri string with your MongoDB deployment's connection string. 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");
Note
For information about connecting to Atlas Serverless, see the Serverless Instance Limitations page for the minimum driver version you need.
See Connect to MongoDB for more ways to connect.
Stable API
You can use the Stable API feature starting with MongoDB Server version 5.0 and Java Reactive Streams Driver version 4.3. When you use the Stable API feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Stable API.
Note
Starting from February 2022, the Versioned API is known as the Stable API. All concepts and features remain the same with this naming change.
To use this feature, construct a MongoDB client instance, specifying a version of the Stable API:
import com.mongodb.ConnectionString; import com.mongodb.MongoClientSettings; import com.mongodb.ServerApi; import com.mongodb.ServerApiVersion; import com.mongodb.reactivestreams.client.MongoClient; import com.mongodb.reactivestreams.client.MongoClients; // ... // Replace <connection string> with your MongoDB deployment's connection string. ConnectionString connString = new ConnectionString("<connection string>"); // Set the Stable API version on the client. ServerApi serverApi = ServerApi.builder() .version(ServerApiVersion.V1) .build(); MongoClientSettings settings = MongoClientSettings.builder() .applyConnectionString(connString) .serverApi(serverApi) .build(); MongoClient mongoClient = MongoClients.create(settings);
Connect to a MongoDB Server on Your Local Machine
If you need to run a MongoDB server on your local machine for development purposes instead of using an Atlas cluster, you need to complete the following:
Download the Community or Enterprise version of MongoDB Server.
Install and configure MongoDB Server.
Start the server.
Important
Always secure your MongoDB server from malicious attacks. See our Security Checklist for a list of security recommendations.
After you successfully start your MongoDB server, specify your connection string in your driver connection code.
If your MongoDB Server is running locally, you can use the connection string
"mongodb://localhost:<port>"
where <port>
is the port number you
configured your server to listen for incoming connections.
If you need to specify a different hostname or IP address, see our Server Manual entry on Connection Strings.
To test whether you can connect to your server, replace the connection string in the Connect to MongoDB Atlas code example and run it.
Compatibility
MongoDB Compatibility
The following compatibility table specifies the recommended version or versions of the MongoDB Reactive Streams driver for use with a specific version of MongoDB.
The first column lists the driver version.
Important
MongoDB ensures compatibility between the MongoDB Server and the drivers for three years after the server version's end of life (EOL) date. To learn more about the MongoDB release and EOL dates, see MongoDB Software Lifecycle Schedules.
Compatibility Table Legend
Icon | Explanation |
---|---|
✓ | All features are supported. |
⊛ | The Driver version will work with the MongoDB version, but not all
new MongoDB features are supported. |
No mark | The Driver version is not tested with the MongoDB version. |
Java Reactive Streams Driver Version | MongoDB 7.0 | MongoDB 6.1 | MongoDB 6.0 | MongoDB 5.0 | MongoDB 4.4 | MongoDB 4.2 | MongoDB 4.0 | MongoDB 3.6 | MongoDB 3.4 | MongoDB 3.2 | MongoDB 3.0 | MongoDB 2.6 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
4.11 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
4.10 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
4.9 | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
4.8 | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||||
4.7 | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.6 | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.5 | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.4 | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.3 | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.2 | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.1 | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
4.0 | ⊛ | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
1.13 | ⊛ | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
1.12 | ⊛ | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
1.11 | ⊛ | ⊛ | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
1.10 | ⊛ | ⊛ | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
1.9 | ⊛ | ⊛ | ⊛ | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
1.7 | ✓ | ✓ | ✓ | ✓ | ✓ | |||||||
1.6 | ✓ | ✓ | ✓ | ✓ | ||||||||
1.5 | ✓ | ✓ | ✓ | ✓ | ||||||||
1.3 | ✓ | ✓ | ✓ | ✓ | ||||||||
1.2 | ✓ | ✓ | ✓ | |||||||||
1.1 | ✓ | ✓ | ||||||||||
1.0 | ✓ | ✓ |
The driver does not support older versions of MongoDB.
Language Compatibility
The following compatibility table specifies the recommended version or versions of the MongoDB Reactive Streams driver for use with a specific version of Java.
The first column lists the driver version.
Java Reactive Streams Driver Version | Java 17 | Java 11 [1] | Java 8 | Java 7 | Java 6 |
---|---|---|---|---|---|
4.11 | ✓ | ✓ | ✓ | ||
4.10 | ✓ | ✓ | ✓ | ||
4.9 | ✓ | ✓ | ✓ | ||
4.8 | ✓ | ✓ | ✓ | ||
4.7 | ✓ | ✓ | ✓ | ||
4.6 | ✓ | ✓ | ✓ | ||
4.5 | ✓ | ✓ | ✓ | ||
4.4 | ✓ | ✓ | ✓ | ||
4.3 | ✓ | ✓ | ✓ | ✓ | |
4.2 | ✓ | ✓ | ✓ | ✓ | |
4.1 | ✓ | ✓ | ✓ | ✓ | |
1.13 | ✓ | ✓ | ✓ | ✓ | |
1.12 | ✓ | ✓ | ✓ | ✓ | |
1.11 | ✓ | ✓ | ✓ | ✓ | |
1.10 | ✓ | ✓ | ✓ | ✓ | |
1.9 | ✓ | ✓ | ✓ | ✓ | |
1.7 | ✓ | ✓ | ✓ | ✓ | |
1.6 | ✓ | ✓ | ✓ | ✓ | |
1.5 | ✓ | ✓ | ✓ | ✓ | |
1.3 | ✓ | ✓ | ✓ | ✓ | |
1.2 | ✓ | ✓ | ✓ | ✓ | |
1.1 | ✓ | ✓ | ✓ | ✓ | |
1.0 | ✓ | ✓ | ✓ | ✓ |
[1] | 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.