Skip to content

Commit e0025f2

Browse files
npentreljeff-allen-mongo
authored andcommitted
DOCS-13063 Clarify $group order behaviour
1 parent 09e4813 commit e0025f2

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-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
@@ -32,11 +32,10 @@ Definition
3232
Behavior
3333
--------
3434

35-
When using :group:`$first` in a :pipeline:`$group` stage, the
36-
:pipeline:`$group` stage should follow a :pipeline:`$sort` stage to
37-
have the input documents in a defined order.
38-
39-
.. include:: /includes/note-group-pipeline-sort-order.rst
35+
When using :group:`$first` in a :pipeline:`$group` stage, the output
36+
value depends on the order of the documents coming into pipeline. To
37+
guarantee a defined order, the :pipeline:`$group` pipeline stage should
38+
follow a :pipeline:`$sort` stage.
4039

4140
Example
4241
-------

source/reference/operator/aggregation/last.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ Definition
3232
Behavior
3333
--------
3434

35-
When using :group:`$last` in a :pipeline:`$group` stage, the
36-
:pipeline:`$group` stage should follow a :pipeline:`$sort` stage to
37-
have the input documents in a defined order.
38-
39-
.. include:: /includes/note-group-pipeline-sort-order.rst
35+
When using :group:`$last` in a :pipeline:`$group` stage, the output
36+
value depends on the order of the documents coming into pipeline. To
37+
guarantee a defined order, the :pipeline:`$group` pipeline stage should
38+
follow a :pipeline:`$sort` stage.
4039

4140
Example
4241
-------

source/reference/operator/aggregation/push.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ Definition
3232
Behavior
3333
--------
3434

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+
3541
Memory Restrictions
3642
~~~~~~~~~~~~~~~~~~~
3743

@@ -67,7 +73,8 @@ compute the list of items and quantities sold for each group:
6773

6874
db.sales.aggregate(
6975
[
70-
{
76+
{ $sort: { date: 1, item: 1 } },
77+
{
7178
$group:
7279
{
7380
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },

0 commit comments

Comments
 (0)