Overview
This section includes guides on how to use each of the available builders and demonstrates the utility that the Java Reactive Streams driver builder classes provide.
The Java Reactive Streams driver includes builder classes for CRUD operations and the Aggregation API. The static utility methods allow you to build queries and other documents more efficiently.
Why Use Builders?
When you use builder classes, you leverage the following features:
The Java compiler to find errors during development
The IDE for discovery and code completion
Builder classes enable the Java compiler and the IDE to catch errors, such as misspelled operators or missing parameters early on. If you use the MongoDB shell or plain Java instead, you write operators as strings and get no visual indication of a problem, which pushes these errors to runtime instead of compile time.
By using builder classes, you can write operators as methods, so that your IDE instantly indicates whether your code has errors. While developing, your IDE can also show you methods that you can use and can complete your code with placeholder parameters.
Example Scenario
Suppose you want to send a marketing email to all users in the users collection that meet the following criteria:
Users in which the value of the
genderfield is"female"Users in which the value of the
agefield is greater than29
You also need your query to return only their email addresses.
The documents in the users collection are modeled by the following class:
record User( @BsonId ObjectId id, String gender, int age, String email )
Using the MongoDB Shell
The following code provides the command you use in the MongoDB Shell to perform the query:
collection.find({ "gender": "female", "age" : { "$gt": 29 }}, { "_id": 0, "email": 1 })
Without Using Builders
The following code provides the find operation you create without builders in the Java Reactive Streams driver:
Bson filter = new Document().append("gender", "female").append("age", new Document().append("$gt", 29)); Bson projection = new Document().append("_id", 0).append("email", 1); Flux.from(collection.find(filter).projection(projection)) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
In this case, you might easily include an error when writing the "\$gt" operator in the filter, but your IDE returns the relevant error only at runtime.
Using Builders
The following code provides the find operation you create by using builders in the Java Reactive Streams driver:
import static com.mongodb.client.model.Filters.*; import static com.mongodb.client.model.Projections.*;
Bson filter = and(eq("gender", "female"), gt("age", 29)); Bson projection = fields(excludeId(), include("email")); Flux.from(collection.find(filter).projection(projection)) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
Available Builders
Aggregates for building aggregation pipelines
Filters for building query filters
Indexes for creating index keys
Projections for building projections
Sorts for building sort criteria
Updates for building updates