|
| 1 | +.. _javars-aggregation: |
| 2 | + |
| 3 | +===================== |
| 4 | +Aggregation Framework |
| 5 | +===================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +The aggregation pipeline is a framework for data aggregation, |
| 14 | +modeled on the concept of data processing pipelines. |
| 15 | + |
| 16 | +To learn more about aggregation, see :manual:`Aggregation Pipeline |
| 17 | +</core/aggregation-pipeline/>` in the Server manual. |
| 18 | + |
| 19 | +Prerequisites |
| 20 | +------------- |
| 21 | + |
| 22 | +You must set up the following components to run the code examples in |
| 23 | +this guide: |
| 24 | + |
| 25 | +- A ``test.restaurants`` collection populated with documents from the |
| 26 | + ``restaurants.json`` file in the `documentation assets GitHub |
| 27 | + <https://raw.githubusercontent.com/mongodb/docs-assets/drivers/restaurants.json>`__. |
| 28 | + |
| 29 | +- The following import statements: |
| 30 | + |
| 31 | +.. code-block:: java |
| 32 | + |
| 33 | + import com.mongodb.reactivestreams.client.MongoClients; |
| 34 | + import com.mongodb.reactivestreams.client.MongoClient; |
| 35 | + import com.mongodb.reactivestreams.client.MongoCollection; |
| 36 | + import com.mongodb.reactivestreams.client.MongoDatabase; |
| 37 | + import com.mongodb.client.model.Aggregates; |
| 38 | + import com.mongodb.client.model.Accumulators; |
| 39 | + import com.mongodb.client.model.Projections; |
| 40 | + import com.mongodb.client.model.Filters; |
| 41 | + |
| 42 | + import org.bson.Document; |
| 43 | + |
| 44 | +.. important:: |
| 45 | + |
| 46 | + This guide uses the ``Subscriber`` implementations, which are |
| 47 | + described in the :ref:`Quick Start Primer <javars-primer>`. |
| 48 | + |
| 49 | +Connect to a MongoDB Deployment |
| 50 | +------------------------------- |
| 51 | + |
| 52 | +First, connect to a MongoDB deployment and declare and define |
| 53 | +``MongoDatabase`` and ``MongoCollection`` instances. |
| 54 | + |
| 55 | +The following code connects to a standalone |
| 56 | +MongoDB deployment running on ``localhost`` on port ``27017``. Then, it |
| 57 | +defines the ``database`` variable to refer to the ``test`` database and |
| 58 | +the ``collection`` variable to refer to the ``restaurants`` collection: |
| 59 | + |
| 60 | +.. code-block:: java |
| 61 | + |
| 62 | + MongoClient mongoClient = MongoClients.create(); |
| 63 | + MongoDatabase database = mongoClient.getDatabase("test"); |
| 64 | + MongoCollection<Document> collection = database.getCollection("restaurants"); |
| 65 | + |
| 66 | +To learn more about connecting to MongoDB deployments, |
| 67 | +see the :ref:`javars-connect` tutorial. |
| 68 | + |
| 69 | +Perform Aggregation |
| 70 | +------------------- |
| 71 | + |
| 72 | +To perform aggregation, pass a list of aggregation stages to the |
| 73 | +``MongoCollection.aggregate()`` method. The driver provides the |
| 74 | +``Aggregates`` helper class that contains builders for aggregation |
| 75 | +stages. |
| 76 | + |
| 77 | +In this example, the aggregation pipeline performs the |
| 78 | +following tasks: |
| 79 | + |
| 80 | +- Uses a ``$match`` stage to filter for documents in which the |
| 81 | + ``categories`` array field contains the element ``"Bakery"``. The example |
| 82 | + uses ``Aggregates.match()`` to build the ``$match`` stage. |
| 83 | + |
| 84 | ++ Uses a ``$group`` stage to group the matching documents by |
| 85 | + the ``stars`` field, accumulating a count of documents for each distinct |
| 86 | + value of ``stars``. The example uses ``Aggregates.group()`` to build the |
| 87 | + ``$group`` stage and ``Accumulators.sum()`` to build the accumulator |
| 88 | + expression. For the accumulator expressions for use within the |
| 89 | + ``$group`` stage, the driver provides ``Accumulators`` helper |
| 90 | + class. |
| 91 | + |
| 92 | +.. code-block:: java |
| 93 | + |
| 94 | + collection.aggregate( |
| 95 | + Arrays.asList( |
| 96 | + Aggregates.match(Filters.eq("categories", "Bakery")), |
| 97 | + Aggregates.group("$stars", Accumulators.sum("count", 1)) |
| 98 | + ) |
| 99 | + ).subscribe(new PrintDocumentSubscriber()); |
| 100 | + |
| 101 | +Use Aggregation Expressions |
| 102 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 103 | + |
| 104 | +For ``$group`` accumulator expressions, the driver provides the |
| 105 | +``Accumulators`` helper class. For other aggregation expressions, |
| 106 | +manually build the expression by using the ``Document`` class. |
| 107 | + |
| 108 | +In the following example, the aggregation pipeline uses a |
| 109 | +``$project`` stage to return only the ``name`` field and the calculated |
| 110 | +field ``firstCategory`` whose value is the first element in the |
| 111 | +``categories`` array. The example uses ``Aggregates.project()`` and |
| 112 | +various ``Projections`` class methods to build the ``$project`` stage: |
| 113 | + |
| 114 | +.. code-block:: java |
| 115 | + |
| 116 | + collection.aggregate( |
| 117 | + Arrays.asList( |
| 118 | + Aggregates.project( |
| 119 | + Projections.fields( |
| 120 | + Projections.excludeId(), |
| 121 | + Projections.include("name"), |
| 122 | + Projections.computed( |
| 123 | + "firstCategory", |
| 124 | + new Document("$arrayElemAt", Arrays.asList("$categories", 0)) |
| 125 | + ) |
| 126 | + ) |
| 127 | + ) |
| 128 | + ) |
| 129 | + ).subscribe(new PrintDocumentSubscriber()); |
| 130 | + |
| 131 | +Explain an Aggregation |
| 132 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 133 | + |
| 134 | +To ``$explain`` an aggregation pipeline, call the |
| 135 | +``AggregatePublisher.explain()`` method: |
| 136 | + |
| 137 | +.. code-block:: java |
| 138 | + |
| 139 | + collection.aggregate( |
| 140 | + Arrays.asList( |
| 141 | + Aggregates.match(Filters.eq("categories", "Bakery")), |
| 142 | + Aggregates.group("$stars", Accumulators.sum("count", 1)))) |
| 143 | + .explain() |
| 144 | + .subscribe(new PrintDocumentSubscriber()); |
0 commit comments