Skip to content

Commit 1f284a7

Browse files
author
Dave Cuthbert
authored
DOCSP-21319 BACKPORT (#1322)
1 parent 797d9ee commit 1f284a7

File tree

1 file changed

+22
-10
lines changed
  • source/reference/operator/aggregation

1 file changed

+22
-10
lines changed

source/reference/operator/aggregation/group.txt

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ Definition
1515

1616
.. pipeline:: $group
1717

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>`.
2331

2432
.. note::
2533

@@ -32,7 +40,7 @@ Definition
3240
{
3341
$group:
3442
{
35-
_id: <expression>, // Group By Expression
43+
_id: <expression>, // Group key
3644
<field1>: { <accumulator1> : <expression1> },
3745
...
3846
}
@@ -46,10 +54,12 @@ Definition
4654
- Description
4755

4856
* - ``_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>`.
5363

5464
* - ``field``
5565
- *Optional.* Computed using the
@@ -119,6 +129,8 @@ the first document of each group.
119129
}
120130
])
121131

132+
.. _ex-agg-group-stage:
133+
122134
Examples
123135
--------
124136

0 commit comments

Comments
 (0)