Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ includes its own setup instructions.
- Hibernate
- Kotlin Coroutine
- Kotlin Sync
- Java Reactive Streams
- [Java Reactive Streams](java-rs/hello-world/README.md)
- Java Sync
- [.NET/C#](dotnet/hello-world/README.md)
- [Node.js](node/hello-world/README.md)
Expand Down
1 change: 1 addition & 0 deletions java-rs/hello-world/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
69 changes: 69 additions & 0 deletions java-rs/hello-world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Get Started with the MongoDB Java Reactive Streams Driver

This sample application connects to a MongoDB deployment, seeds a small
set of sample product documents, and retrieves one of them. Because the
app inserts its own data, you don't need to load an external dataset.

The app uses the MongoDB Reactive Streams Java driver together with
[Project Reactor](https://projectreactor.io/) to consume the `Publisher`
results that the driver returns.

## Prerequisites

Before you begin, complete the [Atlas Get Started guide](https://www.mongodb.com/docs/get-started/)
to create a free Atlas deployment and save your database user
credentials.

You also need the following components installed in your development environment:

- JDK version 21 or later
- Maven

## Installation

Clone this repository:

```bash
git clone https://github.com/mongodb/mongodb-code-examples
```

Install the MongoDB Java Reactive Streams driver. Follow the
[Java Reactive Streams installation guide](https://www.mongodb.com/docs/languages/java/reactive-streams-driver/current/getting-started/)
for your platform.


Navigate into the `java-rs/hello-world` project directory and compile the
application:

```bash
cd mongodb-code-examples/java-rs/hello-world
mvn compile
```

## Connect to MongoDB

Set your connection string as an environment variable, replacing
`<connection string uri>` with your connection string:

```bash
export MONGODB_URI="<connection string uri>"
```

## Run the Application

```bash
mvn compile exec:java
```

When you run the app, it inserts a few product documents into the
`get_started.products` collection, then queries and prints one of them:

```
{"_id": {"$oid": "..."}, "name": "Wireless Mouse", "category": "Electronics", "price": 24.99, "tags": ["wireless", "usb", "ergonomic"]}
```

You can run the app more than once. It clears the collection before
each run, so the results stay consistent.

If you encounter an error or see no output, verify that you set the
`MONGODB_URI` environment variable correctly.
57 changes: 57 additions & 0 deletions java-rs/hello-world/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mongodb</groupId>
<artifactId>hello-world-reactive-streams</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<exec.mainClass>HelloWorld</exec.mainClass>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-bom</artifactId>
<version>5.9.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-bom</artifactId>
<version>2025.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-reactivestreams</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.5.0</version>
</plugin>
</plugins>
</build>
</project>
50 changes: 50 additions & 0 deletions java-rs/hello-world/src/main/java/HelloWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import static com.mongodb.client.model.Filters.eq;

import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
import com.mongodb.reactivestreams.client.MongoCollection;
import com.mongodb.reactivestreams.client.MongoDatabase;
import java.util.List;
import org.bson.Document;
import reactor.core.publisher.Mono;

public class HelloWorld {

// A few sample product documents seeded by this app so you can run
// it without loading an external dataset.
private static final List<Document> SAMPLE_PRODUCTS = List.of(
new Document("name", "Wireless Mouse")
.append("category", "Electronics")
.append("price", 24.99)
.append("tags", List.of("wireless", "usb", "ergonomic")),
new Document("name", "Standing Desk")
.append("category", "Furniture")
.append("price", 349.99)
.append("tags", List.of("adjustable", "office")),
new Document("name", "Noise-Cancelling Headphones")
.append("category", "Electronics")
.append("price", 199.99)
.append("tags", List.of("bluetooth", "wireless", "over-ear"))
);

public static void main(String[] args) {
String uri = System.getenv("MONGODB_URI");

try (MongoClient client = MongoClients.create(uri)) {
MongoDatabase database = client.getDatabase("get_started");
MongoCollection<Document> products = database.getCollection("products");

// Each reactive driver call returns a Publisher. Wrapping it in a
// Reactor Mono and calling block() runs the operation and waits for
// it to complete before moving on.

// Seed the collection so the app has data to query. Clearing the
// collection first keeps results consistent across repeated runs.
Mono.from(products.deleteMany(new Document())).block();
Mono.from(products.insertMany(SAMPLE_PRODUCTS)).block();

Document product = Mono.from(products.find(eq("name", "Wireless Mouse")).first()).block();
System.out.println(product.toJson());
}
}
}