Navigation

MongoDB Java Reactive Streams

On this page

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

Java Reactive Streams is an official Java driver for MongoDB and is the recommended driver for working with reactive streams in the JVM ecosystem.

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 Versioned API feature starting with MongoDB Server version 5.0 and Java Reactive Streams Driver version 4.3. When you use the Versioned API feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Versioned API. To use this feature, construct a MongoDB client instance, specifying a version of the Versioned 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 Versioned 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.

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

The driver does not support older versions of MongoDB.

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

Give Feedback

On this page

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