0% found this document useful (0 votes)
39 views4 pages

mongo-Java-Driver/4.1/../) : Er/blob/master/docs/reference/content/driver/tutorials/aggregation - MD

The document discusses performing aggregation operations in MongoDB using the Java driver. It provides examples of using the aggregation pipeline to match documents, group documents, and project fields. It explains how to connect to a MongoDB deployment and perform aggregation by passing a list of aggregation stages to the MongoCollection aggregate method. The driver provides classes like Aggregates and Accumulators to help build the aggregation stages.

Uploaded by

AMIMUL EHSAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views4 pages

mongo-Java-Driver/4.1/../) : Er/blob/master/docs/reference/content/driver/tutorials/aggregation - MD

The document discusses performing aggregation operations in MongoDB using the Java driver. It provides examples of using the aggregation pipeline to match documents, group documents, and project fields. It explains how to connect to a MongoDB deployment and perform aggregation by passing a list of aggregation stages to the MongoCollection aggregate method. The driver provides classes like Aggregates and Accumulators to help build the aggregation stages.

Uploaded by

AMIMUL EHSAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

 (/mongo-java-driver/4.1/..

/) Search docs 

https://github.com/mongodb/mongo-java-
er/blob/master/docs/reference/content/driver/tutorials/aggregation.md)
Java Driver (/mongo-java-driver/4.1/driver/) Tutorials (/mongo-java-driver/4.1/driver/tutorials/) Aggregation

Aggregation Framework
The aggregation pipeline (http://docs.mongodb.org/manual/core/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 the test database. To create and populate
the collection, follow the directions in github (https://github.com/mongodb/docs-assets/tree/drivers).
Include the following import statements:

import com.mongodb.Block;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.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;

Include the following code which the examples in the tutorials will use to print the results of the
aggregation:

Block<Document> printBlock = new Block<Document>() {


@Override
public void apply(final Document document) {
System.out.println(document.toJson());
}
};

Connect to a MongoDB Deployment


Connect to a MongoDB deployment and declare and define a MongoDatabase and a MongoCollection

instances. (/mongo-java-driver/4.1/../) Search docs 

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 (/mongo-java-


driver/4.1/driver/tutorials/connect-to-mongodb/).

Perform Aggregation
To perform aggregation, pass a list of aggregation stages
(http://docs.mongodb.org/manual/meta/aggregation-quick-reference ) to the
MongoCollection.aggregate() (/mongo-java-driver/4.1/apidocs/mongodb-driver-
sync/com/mongodb/client/MongoCollection.html#aggregate(java.util.List) ) method. The Java driver
provides the Aggregates (/mongo-java-driver/4.1/apidocs/mongodb-driver-
core/com/mongodb/client/model/Aggregates.html ) helper class that contains builders for aggregation
stages.

In the following example, the aggregation pipeline

First uses a $match (http://docs.mongodb.org/manual/reference/operator/aggregation/match/ )


stage to filter for documents whose categories array field contains the element Bakery . The
example uses Aggregates.match (/mongo-java-driver/4.1/builders/aggregation/#match) to build
the $match stage.
Then, uses a $group (http://docs.mongodb.org/manual/reference/operator/aggregation/group/ )
stage to group the matching documents by the stars field, accumulating a count of documents for
each distinct value of stars . The example uses Aggregates.group (/mongo-java-
driver/4.1/builders/aggregation/#group) to build the $group stage and Accumulators.sum
(/mongo-java-driver/4.1/apidocs/mongodb-driver-
core/com/mongodb/client/model/Accumulators#sum(java.lang.String,TExpression).html ) to build
the accumulator expression
(http://docs.mongodb.org/manual/reference/operator/aggregation/group/#accumulator-operator ).
For the accumulator expressions
(http://docs.mongodb.org/manual/reference/operator/aggregation-group/ ) for use within the
$group (http://docs.mongodb.org/manual/reference/operator/aggregation/group/ ) stage, the Java
 (/mongo-java-driver/4.1/../) Search docs
driver provides Accumulators (/mongo-java-driver/4.1/apidocs/mongodb-driver-

core/com/mongodb/client/model/Accumulators.html ) helper class.

collection.aggregate(
Arrays.asList(
Aggregates.match(Filters.eq("categories", "Bakery")),
Aggregates.group("$stars", Accumulators.sum("count", 1))
)
).forEach(printBlock);

Use Aggregation Expressions


For $group accumulator expressions (http://docs.mongodb.org/manual/reference/operator/aggregation-
group/ ), the Java driver provides Accumulators (/mongo-java-driver/4.1/apidocs/mongodb-driver-
core/com/mongodb/client/model/Accumulators.html ) helper class. For other aggregation expressions
(http://docs.mongodb.org/manual/meta/aggregation-quick-reference/#aggregation-expressions ),
manually build the expression Document .

In the following example, the aggregation pipeline uses a $project


(http://docs.mongodb.org/manual/reference/operator/aggregation/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 (/mongo-java-driver/4.1/builders/aggregation/#project) and
various Projections (/mongo-java-driver/4.1/apidocs/mongodb-driver-
core/com/mongodb/client/model/Projections.html ) 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))
)
)
)
)
).forEach(printBlock);

 Write Operations (/mongo-java-driver/4.1/driver/tutorials/perform-write-operations/)


Change Streams  (/mongo-java-driver/4.1/driver/tutorials/change-streams/)
 (/mongo-java-driver/4.1/../) Search docs 

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy