Connect to MongoDB
The Java Reactive Streams driver is an implementation of the Reactive Streams API, which consists of three components:
Publisher
Subscriber
Subscription
A Publisher
is a provider of an unbounded number of sequenced elements, published
according to the demand received from its Subscriber
or multiple instances
of Subscriber
. A Subscription
represents a one-to-one lifecycle of a Subscriber
subscribing to a Publisher
.
Tip
To learn more about reactive streams, visit the Reactive Streams documentation.
In this tutorial, you must implement Java Reactive Streams Subscribers
to
query your database. This guide uses methods from the Reactor library, a library
based on the Reactive Streams specification to implement Java Reactive Streams
Subscribers
.
To learn more about the Reactor library, see Getting Started in the Project Reactor documentation.
Create your Java Reactive Streams driver Application
You must use a project within an integrated development environment (IDE) to complete the following steps.
In your project, create a new Java file in the Java
package named
QueryDatabase
. Copy and paste the following code into the QueryDatabase
file:
import com.mongodb.*; import com.mongodb.reactivestreams.client.MongoCollection; import org.bson.Document; import reactor.core.publisher.Mono; import com.mongodb.reactivestreams.client.MongoClient; import com.mongodb.reactivestreams.client.MongoClients; import com.mongodb.reactivestreams.client.MongoDatabase; import static com.mongodb.client.model.Filters.eq; public class QueryDatabase { public static void main(String[] args) { // Replace the placeholder with your Atlas connection string String uri = "<connection string>"; // Construct a ServerApi instance using the ServerApi.builder() method ServerApi serverApi = ServerApi.builder() .version(ServerApiVersion.V1) .build(); MongoClientSettings settings = MongoClientSettings.builder() .applyConnectionString(new ConnectionString(uri)) .serverApi(serverApi) .build(); // Create a new client and connect to the server try (MongoClient mongoClient = MongoClients.create(settings)) { MongoDatabase database = mongoClient.getDatabase("sample_mflix"); MongoCollection<Document> movies = database.getCollection("movies"); Mono.from(movies.find(eq("title", "Back to the Future"))) .doOnSuccess(i -> System.out.println(i)) .doOnError(err -> System.out.println("Error: " + err.getMessage())) .block(); } } }
Assign the Connection String
Replace the <connection string>
placeholder with the
connection string that you copied from the Create a Connection String
step of this guide.
Run your Application
Run your application, either in your IDE or in your shell. The output shows that you have connected to MongoDB and queried the database.
{ _id: ..., plot: 'A young man is accidentally sent 30 years into the past...', genres: [ 'Adventure', 'Comedy', 'Sci-Fi' ], ... title: 'Back to the Future', ... }
Tip
If you encounter an error, check whether you specified the proper connection string, and that you loaded the sample data.
After you complete these steps, you have a working application that uses the driver to connect to your MongoDB deployment, queries the database, and prints out the result.
Note
If you run into issues on this step, ask for help in the MongoDB Community Forums or submit feedback by using the Rate this page tab on the right or bottom right side of this page.