@@ -15,11 +15,19 @@ Definition
15
15
16
16
.. pipeline:: $group
17
17
18
- Groups input documents by the specified ``_id`` expression and for
19
- each distinct grouping, outputs a document. The ``_id`` field of each
20
- output document contains the unique group by value. The output
21
- documents can also contain computed fields that hold the values of
22
- some :ref:`accumulator expression <accumulators-group>`.
18
+ The ``$group`` stage separates documents into groups according to a
19
+ "group key". The output is one document for each unique group key.
20
+
21
+ A group key is often a field, or group of fields. The group key can
22
+ also be the result of an expression. Use the ``_id`` field in the
23
+ ``$group`` pipeline stage to set the group key. See below for
24
+ :ref:`usage examples <ex-agg-group-stage>`.
25
+
26
+ In the ``$group`` stage output, the ``_id`` field is set to the
27
+ group key for that document.
28
+
29
+ The output documents can also contain additional fields that are
30
+ set using :ref:`accumulator expressions <accumulators-group>`.
23
31
24
32
.. note::
25
33
@@ -32,7 +40,7 @@ Definition
32
40
{
33
41
$group:
34
42
{
35
- _id: <expression>, // Group By Expression
43
+ _id: <expression>, // Group key
36
44
<field1>: { <accumulator1> : <expression1> },
37
45
...
38
46
}
@@ -46,10 +54,12 @@ Definition
46
54
- Description
47
55
48
56
* - ``_id``
49
- - *Required.* If you specify an ``_id`` value of null, or any
50
- other constant value, the ``$group`` stage calculates
51
- accumulated values for all the input documents as a whole.
52
- :ref:`See example of Group by Null <null-example>`.
57
+ - *Required.* The ``_id`` expression specifies the group key.
58
+ If you specify an ``_id`` value of null, or any other
59
+ constant value, the ``$group`` stage returns a single
60
+ document that aggregates values across all of the input
61
+ documents. :ref:`See the Group by Null example
62
+ <null-example>`.
53
63
54
64
* - ``field``
55
65
- *Optional.* Computed using the
@@ -119,6 +129,8 @@ the first document of each group.
119
129
}
120
130
])
121
131
132
+ .. _ex-agg-group-stage:
133
+
122
134
Examples
123
135
--------
124
136
0 commit comments