Transform Your Data with Aggregation
On this page
Overview
In this guide, you can learn how to use the Kotlin Sync driver to perform aggregation operations.
You can use aggregation operations to process data in your MongoDB collections and return computed results. The MongoDB Aggregation framework, which is part of the Query API, is modeled on the concept of a data processing pipeline. Documents enter a pipeline that contains one or more stages, and each stage transforms the documents to output a final aggregated result.
You can think of an aggregation operation as similar to a car factory. A car factory has an assembly line, which contains assembly stations with specialized tools to do specific jobs, like drills and welders. Raw parts enter the factory, and then the assembly line transforms and assembles them into a finished product.
The aggregation pipeline is the assembly line, aggregation stages are the assembly stations, and operator expressions are the specialized tools.
Compare Aggregation and Find Operations
You can use find operations to perform the following actions:
Select which documents to return
Select which fields to return
Sort the results
You can use aggregation operations to perform the following actions:
Perform find operations
Rename fields
Calculate fields
Summarize data
Group values
Limitations
The following limitations apply when using aggregation operations:
Returned documents must not violate the BSON document size limit of 16 megabytes.
Pipeline stages have a memory limit of 100 megabytes by default. You can exceed this limit by using the
allowDiskUse()
method fromAggregateIterable
class.
Important
$graphLookup exception
The $graphLookup stage has a strict
memory limit of 100 megabytes and ignores the allowDiskUse
option.
Aggregation Example
The examples in this section use the restaurants
collection in the sample_restaurants
database from the Atlas sample datasets. To learn how to create a
free MongoDB Atlas cluster and load the sample datasets, see the
Get Started with Atlas guide.
The following Kotlin data class models the documents in this collection:
data class Restaurant( val name: String, val cuisine: String, val borough: String )
Build and Execute an Aggregation Pipeline
To perform an aggregation on the documents in a collection, pass a list of aggregation
stages to the aggregate()
method.
This example outputs a count of the number of bakeries in each borough of New York City. The following code creates aggregation pipeline that contains the following stages:
A $match stage to filter for documents in which the value of the
cuisine
field is"Bakery"
.A $group stage to group the matching documents by the
borough
field, producing a count of documents for each distinct value of that field.
val pipeline = listOf( Aggregates.match(Filters.eq(Restaurant::cuisine.name, "Bakery")), Aggregates.group("\$borough", Accumulators.sum("count", 1)) ) val results = collection.aggregate<Document>(pipeline) results.forEach { result -> println(result) }
Document{{_id=Bronx, count=71}} Document{{_id=Manhattan, count=221}} Document{{_id=Brooklyn, count=173}} Document{{_id=Queens, count=204}} Document{{_id=Staten Island, count=20}} Document{{_id=Missing, count=2}}
Tip
When specifying a group key for the $group
aggregation stage, ensure that you
escape any $
characters by using the \
character.
Explain an Aggregation
To view information about how MongoDB executes your operation, you can
include the $explain
aggregation stage in your pipeline. When MongoDB explains an
operation, it returns execution plans and performance statistics. An execution
plan is a potential way MongoDB can complete an operation.
When you instruct MongoDB to explain an operation, it returns both the
plan MongoDB selected for the operation and any rejected execution plans.
The following code example runs the same aggregation shown in the preceding section
and adds the $explain
stage to output the operation details:
print(collection.aggregate(pipeline).explain())
{ "explainVersion": "2", "queryPlanner": { "namespace": "sample_restaurants.restaurants" "indexFilterSet": false, "parsedQuery": { "cuisine": {"$eq": "Bakery"} }, "queryHash": "865F14C3", "planCacheKey": "0697561B", "optimizedPipeline": true, "maxIndexedOrSolutionsReached": false, "maxIndexedAndSolutionsReached": false, "maxScansToExplodeReached": false, "winningPlan": { ... } ... } ... }
Additional Information
To view a full list of expression operators, see Aggregation Operators in the MongoDB Server manual.
To learn about assembling an aggregation pipeline and view examples, see Aggregation Pipeline in the MongoDB Server manual.
To learn more about creating pipeline stages, see Aggregation Stages in the MongoDB Server manual.
To learn more about explaining MongoDB operations, see Explain Output and Query Plans in the MongoDB Server manual.
API Documentation
For more information about executing aggregation operations with the Kotlin Sync driver, see the following API documentation: