From 4f5b9fb60aae9e123d627300983de6f2bcdf2d68 Mon Sep 17 00:00:00 2001 From: Taylor McNeil Date: Wed, 29 Jul 2026 16:51:39 -0400 Subject: [PATCH 1/2] Porting Getting Started to Java-RS --- README.md | 2 + java-rs/hello-world/.gitignore | 1 + java-rs/hello-world/README.md | 64 +++++++++++++++++++ java-rs/hello-world/pom.xml | 57 +++++++++++++++++ .../hello-world/src/main/java/HelloWorld.java | 50 +++++++++++++++ 5 files changed, 174 insertions(+) create mode 100644 java-rs/hello-world/.gitignore create mode 100644 java-rs/hello-world/README.md create mode 100644 java-rs/hello-world/pom.xml create mode 100644 java-rs/hello-world/src/main/java/HelloWorld.java diff --git a/README.md b/README.md index 9b31392..a2f8894 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ includes its own setup instructions. ## Available Client Libraries - [Go](go/hello-world/README.md) +- [Java](java/hello-world/README.md) +- [Java Reactive Streams](java-rs/hello-world/README.md) - [.NET/C#](dotnet/hello-world/README.md) - [Node.js](node/hello-world/README.md) - [Python](python/hello-world/README.md) diff --git a/java-rs/hello-world/.gitignore b/java-rs/hello-world/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/java-rs/hello-world/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/java-rs/hello-world/README.md b/java-rs/hello-world/README.md new file mode 100644 index 0000000..ae8bb17 --- /dev/null +++ b/java-rs/hello-world/README.md @@ -0,0 +1,64 @@ +# 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 +``` + +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 +`` with your connection string: + +```bash +export MONGODB_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. diff --git a/java-rs/hello-world/pom.xml b/java-rs/hello-world/pom.xml new file mode 100644 index 0000000..b315939 --- /dev/null +++ b/java-rs/hello-world/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + com.mongodb + hello-world-reactive-streams + 1.0.0 + jar + + + 21 + UTF-8 + HelloWorld + + + + + + org.mongodb + mongodb-driver-bom + 5.9.1 + pom + import + + + io.projectreactor + reactor-bom + 2025.0.0 + pom + import + + + + + + + org.mongodb + mongodb-driver-reactivestreams + + + io.projectreactor + reactor-core + + + + + + + org.codehaus.mojo + exec-maven-plugin + 3.5.0 + + + + diff --git a/java-rs/hello-world/src/main/java/HelloWorld.java b/java-rs/hello-world/src/main/java/HelloWorld.java new file mode 100644 index 0000000..9f20bdd --- /dev/null +++ b/java-rs/hello-world/src/main/java/HelloWorld.java @@ -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 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 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()); + } + } +} From 741b01cc64220664fef8cd8e5190f94650aad4d4 Mon Sep 17 00:00:00 2001 From: "Taylor M." Date: Thu, 30 Jul 2026 11:29:07 -0400 Subject: [PATCH 2/2] Update README.md Add missing installation step --- java-rs/hello-world/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/java-rs/hello-world/README.md b/java-rs/hello-world/README.md index ae8bb17..c773241 100644 --- a/java-rs/hello-world/README.md +++ b/java-rs/hello-world/README.md @@ -27,6 +27,11 @@ Clone this repository: 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: