-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
group.txt
258 lines (186 loc) · 8.07 KB
/
group.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
====================
$group (aggregation)
====================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. pipeline:: $group
Groups documents by some specified expression and outputs to the
next stage a document for each distinct grouping. The output
documents contain an ``_id`` field which contains the distinct group
by key. The output documents can also contain computed fields that
hold the values of some accumulator expression grouped by the
:pipeline:`$group`\'s ``_id`` field. :pipeline:`$group` does *not*
order its output documents.
The :pipeline:`$group` stage has the following prototype form:
.. code-block:: javascript
{ $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> }, ... } }
The ``_id`` field is *mandatory*; however, you can specify an
``_id`` value of null to calculate accumulated values for all the
input documents as a whole.
The remaining computed fields are *optional* and computed using the
``<accumulator>`` operators.
The ``_id`` and the ``<accumulator>`` expressions can accept any
valid :ref:`expression <aggregation-expressions>`. For more
information on expressions, see :ref:`aggregation-expressions`.
Considerations
--------------
Accumulator Operator
~~~~~~~~~~~~~~~~~~~~
The ``<accumulator>`` operator must be one of the following accumulator
operators:
.. include:: /includes/toc/table-aggregation-group.rst
.. _group-memory-limit:
``$group`` Operator and Memory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The :pipeline:`$group` stage has a limit of 100 megabytes of RAM. By
default, if the stage exceeds this limit, :pipeline:`$group` will
produce an error. However, to allow for the handling of large datasets,
set the :method:`allowDiskUse <db.collection.aggregate()>` option to
``true`` to enable :pipeline:`$group` operations to write to temporary
files. See :method:`db.collection.aggregate()` method and the
:dbcommand:`aggregate` command for details.
.. versionchanged:: 2.6
MongoDB introduces a limit of 100 megabytes of RAM for the
:pipeline:`$group` stage as well as the :method:`allowDiskUse
<db.collection.aggregate()>` option to handle operations for large
datasets.
Examples
--------
.. _aggregation-group-count:
Calculate Count, Sum, and Average
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Given a collection ``sales`` with the following documents:
.. code-block:: javascript
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-03-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-03-01T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-03-15T09:00:00Z") }
{ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : ISODate("2014-04-04T11:21:39.736Z") }
{ "_id" : 5, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-04-04T21:23:13.331Z") }
Group by Month, Day, and Year
`````````````````````````````
The following aggregation operation uses the :pipeline:`$group` stage
to group the documents by the month, day, and year and calculates the
total price and the average quantity as well as counts the documents
per each group:
.. code-block:: javascript
db.sales.aggregate(
[
{
$group : {
_id : { month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" } },
totalPrice: { $sum: { $multiply: [ "$price", "$quantity" ] } },
averageQuantity: { $avg: "$quantity" },
count: { $sum: 1 }
}
}
]
)
The operation returns the following results:
.. code-block:: javascript
{ "_id" : { "month" : 3, "day" : 15, "year" : 2014 }, "totalPrice" : 50, "averageQuantity" : 10, "count" : 1 }
{ "_id" : { "month" : 4, "day" : 4, "year" : 2014 }, "totalPrice" : 200, "averageQuantity" : 15, "count" : 2 }
{ "_id" : { "month" : 3, "day" : 1, "year" : 2014 }, "totalPrice" : 40, "averageQuantity" : 1.5, "count" : 2 }
Group by ``null``
`````````````````
The following aggregation operation specifies a group ``_id`` of
``null``, calculating the total price and the average quantity as well
as counts for all documents in the collection:
.. code-block:: javascript
db.sales.aggregate(
[
{
$group : {
_id : null,
totalPrice: { $sum: { $multiply: [ "$price", "$quantity" ] } },
averageQuantity: { $avg: "$quantity" },
count: { $sum: 1 }
}
}
]
)
The operation returns the following result:
.. code-block:: javascript
{ "_id" : null, "totalPrice" : 290, "averageQuantity" : 8.6, "count" : 5 }
.. _aggregation-group-distinct-values:
Retrieve Distinct Values
~~~~~~~~~~~~~~~~~~~~~~~~
Given a collection ``sales`` with the following documents:
.. code-block:: javascript
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-03-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-03-01T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-03-15T09:00:00Z") }
{ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : ISODate("2014-04-04T11:21:39.736Z") }
{ "_id" : 5, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-04-04T21:23:13.331Z") }
The following aggregation operation uses the :pipeline:`$group` stage
to group the documents by the item to retrieve the distinct item values:
.. code-block:: javascript
db.sales.aggregate( [ { $group : { _id : "$item" } } ] )
The operation returns the following result:
.. code-block:: javascript
{ "_id" : "xyz" }
{ "_id" : "jkl" }
{ "_id" : "abc" }
.. _aggregation-pivot-data:
Pivot Data
~~~~~~~~~~
A collection ``books`` contains the following documents:
.. code-block:: javascript
{ "_id" : 8751, "title" : "The Banquet", "author" : "Dante", "copies" : 2 }
{ "_id" : 8752, "title" : "Divine Comedy", "author" : "Dante", "copies" : 1 }
{ "_id" : 8645, "title" : "Eclogues", "author" : "Dante", "copies" : 2 }
{ "_id" : 7000, "title" : "The Odyssey", "author" : "Homer", "copies" : 10 }
{ "_id" : 7020, "title" : "Iliad", "author" : "Homer", "copies" : 10 }
Group ``title`` by ``author``
`````````````````````````````
The following aggregation operation pivots the data in the ``books``
collection to have titles grouped by authors.
.. code-block:: javascript
db.books.aggregate(
[
{ $group : { _id : "$author", books: { $push: "$title" } } }
]
)
The operation returns the following documents:
.. code-block:: javascript
{ "_id" : "Homer", "books" : [ "The Odyssey", "Iliad" ] }
{ "_id" : "Dante", "books" : [ "The Banquet", "Divine Comedy", "Eclogues" ] }
.. _group-stage-pivot-using-ROOT:
Group Documents by ``author``
`````````````````````````````
The following aggregation operation uses the :variable:`$$ROOT <ROOT>`
system variable to group the documents by authors. The resulting
documents must not exceed the :limit:`BSON Document Size` limit.
.. code-block:: javascript
db.books.aggregate(
[
{ $group : { _id : "$author", books: { $push: "$$ROOT" } } }
]
)
The operation returns the following documents:
.. code-block:: javascript
{
"_id" : "Homer",
"books" :
[
{ "_id" : 7000, "title" : "The Odyssey", "author" : "Homer", "copies" : 10 },
{ "_id" : 7020, "title" : "Iliad", "author" : "Homer", "copies" : 10 }
]
}
{
"_id" : "Dante",
"books" :
[
{ "_id" : 8751, "title" : "The Banquet", "author" : "Dante", "copies" : 2 },
{ "_id" : 8752, "title" : "Divine Comedy", "author" : "Dante", "copies" : 1 },
{ "_id" : 8645, "title" : "Eclogues", "author" : "Dante", "copies" : 2 }
]
}
.. seealso:: The :doc:`/tutorial/aggregation-zip-code-data-set`
tutorial provides an extensive example of the :pipeline:`$group`
operator in a common use case.