- Reactive Streams
- Tutorials
- Aggregation
Aggregation Framework
The aggregation pipeline is a framework for data aggregation, modeled on the concept of data processing pipelines.
Prerequisites
The example below requires a
restaurants
collection in thetest
database. To create and populate the collection, follow the directions in github.Include the following import statements:
import com.mongodb.reactivestreams.client.MongoClients; import com.mongodb.reactivestreams.client.MongoClient; import com.mongodb.reactivestreams.client.MongoCollection; import com.mongodb.reactivestreams.client.MongoDatabase; import com.mongodb.client.model.Aggregates; import com.mongodb.client.model.Accumulators; import com.mongodb.client.model.Projections; import com.mongodb.client.model.Filters; import org.bson.Document;
important
This guide uses the Subscriber
implementations as covered in the Quick Start Primer.
Connect to a MongoDB Deployment
Connect to a MongoDB deployment and declare and define a MongoDatabase
and a MongoCollection
instances.
For example, include the following code to connect to a standalone MongoDB deployment running on localhost on port 27017
and define database
to refer to the test
database and collection
to refer to the restaurants
collection.
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("restaurants");
For additional information on connecting to MongoDB, see Connect to MongoDB.
Perform Aggregation
To perform aggregation, pass a list of aggregation stages to the MongoCollection.aggregate()
method.
The Java driver provides the Aggregates
helper class that contains builders for aggregation stages.
In the following example, the aggregation pipeline
First uses a
$match
stage to filter for documents whosecategories
array field contains the elementBakery
. The example usesAggregates.match
to build the$match
stage.Then, uses a
$group
stage to group the matching documents by thestars
field, accumulating a count of documents for each distinct value ofstars
. The example usesAggregates.group
to build the$group
stage andAccumulators.sum
to build the accumulator expression. For the accumulator expressions for use within the$group
stage, the Java driver providesAccumulators
helper class.
collection.aggregate(
Arrays.asList(
Aggregates.match(Filters.eq("categories", "Bakery")),
Aggregates.group("$stars", Accumulators.sum("count", 1))
)
).subscribe(new PrintDocumentSubscriber());
Use Aggregation Expressions
For $group accumulator expressions, the Java driver provides Accumulators
helper class. For other aggregation expressions, manually build the expression Document
.
In the following example, the aggregation pipeline uses a $project
stage to return only the name
field and the calculated field firstCategory
whose value is the first element in the categories
array. The example uses Aggregates.project
and various
Projections
methods to build the $project
stage.
collection.aggregate(
Arrays.asList(
Aggregates.project(
Projections.fields(
Projections.excludeId(),
Projections.include("name"),
Projections.computed(
"firstCategory",
new Document("$arrayElemAt", Arrays.asList("$categories", 0))
)
)
)
)
).subscribe(new PrintDocumentSubscriber());
Explain an Aggregation
To explain an aggregation pipeline, call the
AggregatePublisher.explain()
method:
collection.aggregate(
Arrays.asList(
Aggregates.match(Filters.eq("categories", "Bakery")),
Aggregates.group("$stars", Accumulators.sum("count", 1))))
.explain()
.subscribe(new PrintDocumentSubscriber());
The driver supports explain of aggregation pipelines starting with MongoDB 3.6.