Docs Menu

MongoDB Java Reactive Streams

On this page

  • Introduction
  • Installation
  • Connect to MongoDB Atlas
  • Stable API
  • Connect to a MongoDB Server on Your Local Machine
  • Compatibility

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.

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.

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.

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 Feburary 2022, the Versioned API is known 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 version of the {+stable-api+} 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);

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:

  1. Download the Community or Enterprise version of MongoDB Server.
  2. Install and configure MongoDB Server.
  3. 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.

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

The first column lists the driver version(s).

Java Reactive Streams Driver Version
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.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.

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

The first column lists the driver version(s).

Java Reactive Streams Driver Version
Java 17
Java 11 [1]
Java 8
Java 7
Java 6
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.

←  MongoDB Java DriversMongoDB PHP Driver →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.