-
Notifications
You must be signed in to change notification settings - Fork 10
DOCSP-41145: Aggregation landing page #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
.. _kotlin-sync-aggregation: | ||
|
||
==================================== | ||
Transform Your Data with Aggregation | ||
==================================== | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: code example, transform, computed, pipeline | ||
:description: Learn how to use the Kotlin Sync driver to perform aggregation operations. | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. .. toctree:: | ||
.. :titlesonly: | ||
.. :maxdepth: 1 | ||
|
||
.. /aggregation/aggregation-tutorials | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to use the {+driver-short+} to perform | ||
**aggregation operations**. | ||
|
||
Aggregation operations 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 data processing | ||
pipelines. Documents enter a pipeline that contains one or more stages, | ||
and this pipeline transforms the documents into an aggregated result. | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
An aggregation operation is 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. | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The **aggregation pipeline** is the assembly line, **aggregation stages** are the | ||
assembly stations, and **operator expressions** are the | ||
specialized tools. | ||
|
||
Aggregation Versus Find Operations | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
~~~~~~~~~~~ | ||
|
||
Keep the following limitations in mind when using aggregation operations: | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- Returned documents must not violate the | ||
:manual:`BSON document size limit </reference/limits/#mongodb-limit-BSON-Document-Size>` | ||
of 16 megabytes. | ||
- Pipeline stages have a memory limit of 100 megabytes by default. You can exceed this | ||
limit by using the ``allowDiskUse`` method of the | ||
``AggregateIterable`` type. | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. important:: $graphLookup exception | ||
|
||
The :manual:`$graphLookup | ||
</reference/operator/aggregation/graphLookup/>` stage has a strict | ||
memory limit of 100 megabytes and ignores the ``allowDiskUse`` parameter. | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Aggregation Example | ||
------------------- | ||
|
||
The examples in this section use the ``restaurants`` collection in the ``sample_restaurants`` | ||
database from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a | ||
free MongoDB Atlas cluster and load the sample datasets, see the | ||
:atlas:`Get Started with Atlas </getting-started>` guide. | ||
|
||
The following {+language+} data class models the documents in this collection: | ||
|
||
.. literalinclude:: /includes/aggregation/aggregation.kt | ||
:start-after: start-data-class | ||
:end-before: end-data-class | ||
:language: kotlin | ||
:copyable: | ||
|
||
Build and Execute an Aggregation Pipeline | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
To perform an aggregation, pass a list of aggregation stages to the | ||
``collection.aggregate()`` method. | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The following code example produces a count of the number of bakeries in each borough | ||
of New York. To do so, it uses an aggregation pipeline with the following stages: | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- A :manual:`$match </reference/operator/aggregation/match/>` stage to filter for documents | ||
whose ``cuisine`` field contains the value ``"Bakery"``. | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- A :manual:`$group </reference/operator/aggregation/group/>` stage to group the matching | ||
documents by the ``borough`` field, accumulating a count of documents for each distinct | ||
value. | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: add a note here (can be commented out as a TODO) that says the following example uses Aggregates builders. |
||
.. io-code-block:: | ||
|
||
.. input:: /includes/aggregation/aggregation.kt | ||
:language: kotlin | ||
:start-after: start-aggregation-pipeline | ||
:end-before: end-aggregation-pipeline | ||
:dedent: | ||
|
||
.. output:: | ||
:visible: false | ||
|
||
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}} | ||
|
||
Explain an Aggregation | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
To view information about how MongoDB executes your operation, you can | ||
instruct MongoDB to **explain** it. 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 executed and any rejected execution plans. | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The following code example runs the preceding aggregation example and prints the returned | ||
explanation: | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. io-code-block:: | ||
|
||
.. input:: /includes/aggregation/aggregation.kt | ||
:language: kotlin | ||
:start-after: start-aggregation-explain | ||
:end-before: end-aggregation-explain | ||
:dedent: | ||
|
||
.. output:: | ||
:visible: false | ||
|
||
{ | ||
"explain": { | ||
"aggregate": "restaurants", | ||
"pipeline": [ | ||
{ | ||
"$match": { | ||
"cuisine": "Bakery" | ||
} | ||
}, | ||
{ | ||
"$group": { | ||
"_id": "$borough", | ||
"count": { | ||
"$sum": 1 | ||
} | ||
} | ||
} | ||
], | ||
"cursor": {} | ||
}, | ||
... | ||
} | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Additional Information | ||
---------------------- | ||
|
||
To view a full list of expression operators, see :manual:`Aggregation | ||
Operators. </reference/operator/aggregation/>` | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
To learn about assembling an aggregation pipeline and view examples, see | ||
:manual:`Aggregation Pipeline. </core/aggregation-pipeline/>` | ||
|
||
To learn more about creating pipeline stages, see :manual:`Aggregation | ||
Stages. </reference/operator/aggregation-pipeline/>` | ||
|
||
To learn more about explaining MongoDB operations, see | ||
:manual:`Explain Output </reference/explain-results/>` and | ||
:manual:`Query Plans. </core/query-plans/>` | ||
|
||
.. Aggregation Tutorials | ||
.. ~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. To view step-by-step explanations of common aggregation tasks, see | ||
.. :ref:`kotlin-sync-aggregation-tutorials`. | ||
|
||
API Documentation | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
For more information about executing aggregation operations with the {+driver-short+}, | ||
see the following API documentation: | ||
|
||
- `aggregate() <{+api+}/com.mongodb.kotlin.client/-mongo-collection/aggregate.html>`__ | ||
- `AggregateIterable <{+api+}/com.mongodb.kotlin.client/-aggregate-iterable/index.html>`__ | ||
Comment on lines
+219
to
+220
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for reviewer: these links are broken at the moment. Rea has a change that has yet to be merged that changes the |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import com.mongodb.ConnectionString | ||
import com.mongodb.MongoClientSettings | ||
import com.mongodb.client.model.Accumulators | ||
import com.mongodb.client.model.Aggregates | ||
import com.mongodb.client.model.Filters | ||
import com.mongodb.kotlin.client.MongoClient | ||
import org.bson.Document | ||
|
||
// start-data-class | ||
data class Restaurant( | ||
val name: String, | ||
val cuisine: String, | ||
val borough: String | ||
) | ||
// end-data-class | ||
|
||
fun main() { | ||
val uri = "<connection string URI>" | ||
|
||
val settings = MongoClientSettings.builder() | ||
.applyConnectionString(ConnectionString(uri)) | ||
.retryWrites(true) | ||
.build() | ||
|
||
val mongoClient = MongoClient.create(settings) | ||
val database = mongoClient.getDatabase("sample_restaurants") | ||
val collection = database.getCollection<Restaurant>("restaurants") | ||
|
||
// start-aggregation-pipeline | ||
val pipeline = listOf( | ||
Aggregates.match(Filters.eq(Restaurant::cuisine.name, "Bakery")), | ||
Aggregates.group("\$borough", Accumulators.sum("count", 1)) | ||
mcmorisi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
val results = collection.aggregate<Document>(pipeline) | ||
|
||
results.forEach { result -> | ||
println(result) | ||
} | ||
// end-aggregation-pipeline | ||
|
||
// start-aggregation-explain | ||
print(collection.aggregate(pipeline).explain()) | ||
// end-aggregation-explain | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.