Skip to content

Commit a62c15e

Browse files
npentreljeff-allen-mongo
authored andcommitted
DOCS-13063 Clarify $group order behaviour
1 parent 214fdb2 commit a62c15e

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

source/includes/note-group-pipeline-sort-order.rst

Lines changed: 0 additions & 5 deletions
This file was deleted.

source/reference/operator/aggregation/first.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ For more information on expressions, see :ref:`aggregation-expressions`.
4343
Behavior
4444
--------
4545

46-
When using :group:`$first` in a :pipeline:`$group` stage, the
47-
:pipeline:`$group` stage should follow a :pipeline:`$sort` stage to
48-
have the input documents in a defined order.
49-
50-
.. include:: /includes/note-group-pipeline-sort-order.rst
46+
When using :group:`$first` in a :pipeline:`$group` stage, the output
47+
value depends on the order of the documents coming into pipeline. To
48+
guarantee a defined order, the :pipeline:`$group` pipeline stage should
49+
follow a :pipeline:`$sort` stage.
5150

5251
Example
5352
-------

source/reference/operator/aggregation/last.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ For more information on expressions, see :ref:`aggregation-expressions`.
4343
Behavior
4444
--------
4545

46-
When using :group:`$last` in a :pipeline:`$group` stage, the
47-
:pipeline:`$group` stage should follow a :pipeline:`$sort` stage to
48-
have the input documents in a defined order.
49-
50-
.. include:: /includes/note-group-pipeline-sort-order.rst
46+
When using :group:`$last` in a :pipeline:`$group` stage, the output
47+
value depends on the order of the documents coming into pipeline. To
48+
guarantee a defined order, the :pipeline:`$group` pipeline stage should
49+
follow a :pipeline:`$sort` stage.
5150

5251
Example
5352
-------

source/reference/operator/aggregation/push.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ Definition
2929

3030
For more information on expressions, see :ref:`aggregation-expressions`.
3131

32+
Behavior
33+
--------
34+
35+
When using :group:`$push` in a :pipeline:`$group` stage, the order of
36+
the documents in the output array is determined by the order of the
37+
documents coming into pipeline. To guarantee a defined order, the
38+
:pipeline:`$group` pipeline stage should follow a :pipeline:`$sort`
39+
stage.
40+
3241
Example
3342
-------
3443

@@ -52,7 +61,8 @@ compute the list of items and quantities sold for each group:
5261

5362
db.sales.aggregate(
5463
[
55-
{
64+
{ $sort: { date: 1, item: 1 } },
65+
{
5666
$group:
5767
{
5868
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },

0 commit comments

Comments
 (0)