|
| 1 | +.. _node-aggregation-filtered-subset: |
| 2 | + |
| 3 | +=============== |
| 4 | +Filtered Subset |
| 5 | +=============== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: tutorial |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: code example, node.js, sort, limit, aggregation |
| 19 | + |
| 20 | +Introduction |
| 21 | +------------ |
| 22 | + |
| 23 | +In this tutorial, you can learn how to use the {+driver-short+} to |
| 24 | +construct an aggregation pipeline, perform the |
| 25 | +aggregation on a collection, and print the results by completing and |
| 26 | +running a sample app. This aggregation performs the following operations: |
| 27 | + |
| 28 | +- Matches a subset of documents by a field value |
| 29 | +- Formats result documents |
| 30 | + |
| 31 | +.. tip:: |
| 32 | + |
| 33 | + You can also query for a subset of documents in a collection by using the |
| 34 | + Query API. To learn how to specify a query, see the |
| 35 | + :ref:`Read Operations guides <node-read-operations>`. |
| 36 | + |
| 37 | +Aggregation Task Summary |
| 38 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 39 | + |
| 40 | +This tutorial demonstrates how to query a collection for a specific |
| 41 | +subset of documents in a collection. The results contain |
| 42 | +documents that describe the three youngest people who are engineers. |
| 43 | + |
| 44 | +This example uses one collection, ``persons``, which contains |
| 45 | +documents describing people. Each document includes a person's name, |
| 46 | +date of birth, vocation, and other details. |
| 47 | + |
| 48 | +Before You Get Started |
| 49 | +---------------------- |
| 50 | + |
| 51 | +Before you start this tutorial, complete the |
| 52 | +:ref:`node-agg-tutorial-template-app` instructions to set up a working |
| 53 | +Node.js application. |
| 54 | + |
| 55 | +After you set up the app, access the ``persons`` collection by adding the |
| 56 | +following code to the application: |
| 57 | + |
| 58 | +.. literalinclude:: /includes/aggregation/filtered-subset.js |
| 59 | + :language: javascript |
| 60 | + :copyable: true |
| 61 | + :start-after: start-collection |
| 62 | + :end-before: end-collection |
| 63 | + :dedent: |
| 64 | + |
| 65 | +Delete any existing data in the collections and insert sample data into |
| 66 | +the ``persons`` collection as shown in the following code: |
| 67 | + |
| 68 | +.. literalinclude:: /includes/aggregation/filtered-subset.js |
| 69 | + :language: javascript |
| 70 | + :copyable: true |
| 71 | + :start-after: start-insert-persons |
| 72 | + :end-before: end-insert-persons |
| 73 | + :dedent: |
| 74 | + |
| 75 | +Tutorial |
| 76 | +-------- |
| 77 | + |
| 78 | +.. procedure:: |
| 79 | + :style: connected |
| 80 | + |
| 81 | + .. step:: Add a match stage for people who are engineers |
| 82 | + |
| 83 | + First, add a :manual:`$match |
| 84 | + </reference/operator/aggregation/match>` stage that finds documents in which |
| 85 | + the value of the ``vocation`` field is ``"ENGINEER"``: |
| 86 | + |
| 87 | + .. literalinclude:: /includes/aggregation/filtered-subset.js |
| 88 | + :language: javascript |
| 89 | + :copyable: true |
| 90 | + :start-after: start-match |
| 91 | + :end-before: end-match |
| 92 | + :dedent: |
| 93 | + |
| 94 | + .. step:: Add a sort stage to sort from youngest to oldest |
| 95 | + |
| 96 | + Next, add a :manual:`$sort |
| 97 | + </reference/operator/aggregation/sort>` stage that sorts the |
| 98 | + documents in descending order by the ``dateofbirth`` field to |
| 99 | + list the youngest people first: |
| 100 | + |
| 101 | + .. literalinclude:: /includes/aggregation/filtered-subset.js |
| 102 | + :language: javascript |
| 103 | + :copyable: true |
| 104 | + :start-after: start-sort |
| 105 | + :end-before: end-sort |
| 106 | + :dedent: |
| 107 | + |
| 108 | + .. step:: Add a limit stage to see only three results |
| 109 | + |
| 110 | + Next, add a :manual:`$limit </reference/operator/aggregation/limit>` |
| 111 | + stage to the pipeline to output only the first three documents in |
| 112 | + the results. |
| 113 | + |
| 114 | + .. literalinclude:: /includes/aggregation/filtered-subset.js |
| 115 | + :language: javascript |
| 116 | + :copyable: true |
| 117 | + :start-after: start-limit |
| 118 | + :end-before: end-limit |
| 119 | + :dedent: |
| 120 | + |
| 121 | + .. step:: Add an unset stage to remove unneeded fields |
| 122 | + |
| 123 | + Finally, add an :manual:`$unset |
| 124 | + </reference/operator/aggregation/unset>` stage. The |
| 125 | + ``$unset`` stage removes unnecessary fields from the result documents: |
| 126 | + |
| 127 | + .. literalinclude:: /includes/aggregation/filtered-subset.js |
| 128 | + :language: javascript |
| 129 | + :copyable: true |
| 130 | + :start-after: start-unset |
| 131 | + :end-before: end-unset |
| 132 | + :dedent: |
| 133 | + |
| 134 | + .. tip:: |
| 135 | + |
| 136 | + Use the ``$unset`` operator instead of ``$project`` to avoid |
| 137 | + modifying the aggregation pipeline if documents with |
| 138 | + different fields are added to the collection. |
| 139 | + |
| 140 | + .. step:: Run the aggregation pipeline |
| 141 | + |
| 142 | + Add the following code to the end of your application to perform |
| 143 | + the aggregation on the ``persons`` collection: |
| 144 | + |
| 145 | + .. literalinclude:: /includes/aggregation/filtered-subset.js |
| 146 | + :language: javascript |
| 147 | + :copyable: true |
| 148 | + :start-after: start-run-agg |
| 149 | + :end-before: end-run-agg |
| 150 | + :dedent: |
| 151 | + |
| 152 | + Finally, run the following command in your shell to start your |
| 153 | + application: |
| 154 | + |
| 155 | + .. code-block:: bash |
| 156 | + |
| 157 | + node agg_tutorial.js |
| 158 | + |
| 159 | + .. step:: Interpret results |
| 160 | + |
| 161 | + The aggregated result contains three documents. The documents |
| 162 | + represent the three youngest people with the vocation of ``"ENGINEER"``, |
| 163 | + ordered from youngest to oldest. The results omit the ``_id`` and ``address`` |
| 164 | + fields. |
| 165 | + |
| 166 | + .. code-block:: javascript |
| 167 | + :copyable: false |
| 168 | + |
| 169 | + { |
| 170 | + person_id: '7363626383', |
| 171 | + firstname: 'Carl', |
| 172 | + lastname: 'Simmons', |
| 173 | + dateofbirth: 1998-12-26T13:13:55.000Z, |
| 174 | + vocation: 'ENGINEER' |
| 175 | + } |
| 176 | + { |
| 177 | + person_id: '1723338115', |
| 178 | + firstname: 'Olive', |
| 179 | + lastname: 'Ranieri', |
| 180 | + dateofbirth: 1985-05-12T23:14:30.000Z, |
| 181 | + gender: 'FEMALE', |
| 182 | + vocation: 'ENGINEER' |
| 183 | + } |
| 184 | + { |
| 185 | + person_id: '6392529400', |
| 186 | + firstname: 'Elise', |
| 187 | + lastname: 'Smith', |
| 188 | + dateofbirth: 1972-01-13T09:32:07.000Z, |
| 189 | + vocation: 'ENGINEER' |
| 190 | + } |
| 191 | + |
| 192 | +To view the complete code for this tutorial, see the `Completed Filtered Subset App |
| 193 | +<https://github.com/mongodb/docs-node/tree/master/source/includes/aggregation/filtered-subset.js>`__ |
| 194 | +on GitHub. |
0 commit comments