@@ -9,5 +9,140 @@ Run Aggregation Pipelines
99.. contents:: On this page
1010 :local:
1111 :backlinks: none
12- :depth: 1
12+ :depth: 2
1313 :class: singlecol
14+
15+ You can run :manual:`aggregation pipelines </aggregation>` on your
16+ collections in |vsce|. Aggregation pipelines consist of
17+ :manual:`stages </reference/operator/aggregation-pipeline/>` that
18+ process your data and return computed results.
19+
20+ Common uses for aggregation include:
21+
22+ - Grouping data by a given expression.
23+
24+ - Calculating results based on multiple fields and storing those results
25+ in a new field.
26+
27+ - Filtering data to return a subset that matches a given criteria.
28+
29+ - Sorting data.
30+
31+ When you run an aggregation, |vsce| conveniently outputs the
32+ results directly within Visual Studio Code.
33+
34+ Open a Playground to Run Aggregation Pipelines
35+ ----------------------------------------------
36+
37+ You can run aggregation pipelines in a MongoDB Playground. MongoDB
38+ Playgrounds are JavaScript environments where you can prototype queries,
39+ aggregations, and MongoDB commands with helpful syntax highlighting.
40+
41+ To open a new MongoDB Playground:
42+
43+ .. include:: /includes/steps/open-new-playground.rst
44+
45+ Create and Run an Aggregation Pipeline
46+ --------------------------------------
47+
48+ To create an aggregation pipeline, use the following syntax in your
49+ Playground:
50+
51+ .. code-block::
52+
53+ db.<collection>.aggregate([
54+ {
55+ <$stage1>
56+ },
57+ {
58+ <$stage2>
59+ }
60+ ...
61+ ])
62+
63+ To run your Playground, press the :guilabel:`Play Button` at the top
64+ right of the Playground View. |vsce| outputs the results of your
65+ playground to the :guilabel:`Output` view in Visual Studio Code.
66+
67+ .. _agg-pipeline-example:
68+
69+ Example
70+ ~~~~~~~
71+
72+ To use this example, **start with a blank MongoDB Playground** by
73+ clearing the template Playground if it is loaded.
74+
75+ Consider the following playground which inserts sample data into a
76+ collection and aggregates that data:
77+
78+ .. code-block:: javascript
79+
80+ use("test");
81+
82+ db.sales.insertMany([
83+ { "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : new Date("2014-03-01T08:00:00Z") },
84+ { "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : new Date("2014-03-01T09:00:00Z") },
85+ { "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : new Date("2014-03-15T09:00:00Z") },
86+ { "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : new Date("2014-04-04T11:21:39.736Z") },
87+ { "_id" : 5, "item" : "abc", "price" : 10, "quantity" : 10, "date" : new Date("2014-04-04T21:23:13.331Z") },
88+ { "_id" : 6, "item" : "def", "price" : 7.5, "quantity": 5, "date" : new Date("2015-06-04T05:08:13Z") },
89+ { "_id" : 7, "item" : "def", "price" : 7.5, "quantity": 10, "date" : new Date("2015-09-10T08:43:00Z") },
90+ { "_id" : 8, "item" : "abc", "price" : 10, "quantity" : 5, "date" : new Date("2016-02-06T20:20:13Z") }
91+ ])
92+
93+ db.sales.aggregate([
94+ { $match: { date: { $gte: new Date("2014-01-01"), $lt: new Date("2015-01-01") } } },
95+ { $group: { _id: "$item", totalSaleAmount: { $sum: { $multiply: [ "$price", "$quantity" ] } } } }
96+ ])
97+
98+ This pipeline:
99+
100+ 1. Switches to the ``test`` database.
101+
102+ #. Inserts eight documents into the ``test.sales`` collection.
103+
104+ #. Performs an aggregation in two stages:
105+
106+ |
107+
108+ First Stage
109+ The :pipeline:`$match` stage filters the data such that only sales
110+ from the year 2014 are passed to the next stage.
111+
112+ Second Stage
113+ The :pipeline:`$group` stage groups the data by item. The stage
114+ adds a new field to the output called ``totalSaleAmount``, which
115+ is the culmination of the item's ``price`` and ``quantity``.
116+
117+ When you press the :guilabel:`Play Button`, this operation outputs the
118+ following documents to the :guilabel:`Output` view in Visual Studio
119+ Code:
120+
121+ .. code-block:: javascript
122+ :copyable: false
123+
124+ [
125+ {
126+ _id: 'abc',
127+ totalSaleAmount: 120
128+ },
129+ {
130+ _id: 'jkl',
131+ totalSaleAmount: 20
132+ },
133+ {
134+ _id: 'xyz',
135+ totalSaleAmount: 150
136+ }
137+ ]
138+
139+ .. seealso::
140+
141+ - To learn more about the available aggregation stages, see
142+ :manual:`Aggregation Pipeline Stages
143+ </reference/operator/aggregation-pipeline/>`.
144+
145+ - To learn more about the available aggregation operators you
146+ can use within stages, see
147+ :manual:`Aggregation Pipeline Operators
148+ </reference/operator/aggregation/>`.
0 commit comments