|
| 1 | +.. _scala-aggregation: |
| 2 | + |
| 3 | +===================== |
| 4 | +Aggregation Framework |
| 5 | +===================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, create insights, change data |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +The aggregation pipeline is a framework for data aggregation, |
| 21 | +modeled on the concept of data processing pipelines. |
| 22 | + |
| 23 | +To learn more about aggregation, see :manual:`Aggregation Pipeline |
| 24 | +</core/aggregation-pipeline/>` in the Server manual. |
| 25 | + |
| 26 | +Prerequisites |
| 27 | +------------- |
| 28 | + |
| 29 | +.. include:: /includes/prereq-restaurants.rst |
| 30 | + |
| 31 | +.. code-block:: scala |
| 32 | + |
| 33 | + import org.mongodb.scala._ |
| 34 | + |
| 35 | + import org.mongodb.scala.model.Aggregates._ |
| 36 | + import org.mongodb.scala.model.Accumulators._ |
| 37 | + import org.mongodb.scala.model.Filters._ |
| 38 | + import org.mongodb.scala.model.Projections._ |
| 39 | + |
| 40 | +.. include:: /includes/obs-note.rst |
| 41 | + |
| 42 | +Connect to a MongoDB Deployment |
| 43 | +------------------------------- |
| 44 | + |
| 45 | +.. include:: /includes/connect-section.rst |
| 46 | + |
| 47 | +Perform Aggregation |
| 48 | +------------------- |
| 49 | + |
| 50 | +To perform aggregation, pass a list of aggregation stages to the |
| 51 | +``MongoCollection.aggregate()`` method. The driver provides the |
| 52 | +``Aggregates`` helper class that contains builders for aggregation |
| 53 | +stages. |
| 54 | + |
| 55 | +In this example, the aggregation pipeline performs the |
| 56 | +following tasks: |
| 57 | + |
| 58 | +- Uses a ``$match`` stage to filter for documents in which the |
| 59 | + ``categories`` array field contains the element ``"Bakery"``. The example |
| 60 | + uses ``Aggregates.filter()`` to build the ``$match`` stage. |
| 61 | + |
| 62 | ++ Uses a ``$group`` stage to group the matching documents by |
| 63 | + the ``stars`` field, accumulating a count of documents for each distinct |
| 64 | + value of ``stars``. The example uses ``Aggregates.group()`` to build the |
| 65 | + ``$group`` stage and ``Accumulators.sum()`` to build the accumulator |
| 66 | + expression. For the accumulator expressions for use within the |
| 67 | + ``$group`` stage, the driver provides ``Accumulators`` helper |
| 68 | + class. |
| 69 | + |
| 70 | +.. code-block:: scala |
| 71 | + |
| 72 | + collection.aggregate(Seq( |
| 73 | + Aggregates.filter(Filters.equal("categories", "Bakery")), |
| 74 | + Aggregates.group("$stars", Accumulators.sum("count", 1)) |
| 75 | + )).printResults() |
| 76 | + |
| 77 | +Use Aggregation Expressions |
| 78 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 79 | + |
| 80 | +For ``$group`` accumulator expressions, the driver provides the |
| 81 | +``Accumulators`` helper class. For other aggregation expressions, |
| 82 | +manually build the expression by using the ``Document`` class. |
| 83 | + |
| 84 | +In the following example, the aggregation pipeline uses a |
| 85 | +``$project`` stage to return only the ``name`` field and the calculated |
| 86 | +field ``firstCategory`` whose value is the first element in the |
| 87 | +``categories`` array. The example uses ``Aggregates.project()`` and |
| 88 | +various ``Projections`` class methods to build the ``$project`` stage: |
| 89 | + |
| 90 | +.. code-block:: scala |
| 91 | + |
| 92 | + collection.aggregate( |
| 93 | + Seq( |
| 94 | + Aggregates.project( |
| 95 | + Projections.fields( |
| 96 | + Projections.excludeId(), |
| 97 | + Projections.include("name"), |
| 98 | + Projections.computed( |
| 99 | + "firstCategory", |
| 100 | + Document("$arrayElemAt"-> Seq("$categories", 0)) |
| 101 | + ) |
| 102 | + ) |
| 103 | + ) |
| 104 | + ) |
| 105 | + ).printResults() |
| 106 | + |
| 107 | +Explain an Aggregation |
| 108 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 109 | + |
| 110 | +To ``$explain`` an aggregation pipeline, call the |
| 111 | +``AggregatePublisher.explain()`` method: |
| 112 | + |
| 113 | +.. code-block:: scala |
| 114 | + |
| 115 | + collection.aggregate( |
| 116 | + Seq(Aggregates.filter(Filters.eq("categories", "Bakery")), |
| 117 | + Aggregates.group("$stars", Accumulators.sum("count", 1))) |
| 118 | + ).explain().printResults() |
0 commit comments