diff --git a/source/includes/note-group-pipeline-sort-order.rst b/source/includes/note-group-pipeline-sort-order.rst deleted file mode 100644 index 925b93a3503..00000000000 --- a/source/includes/note-group-pipeline-sort-order.rst +++ /dev/null @@ -1,5 +0,0 @@ -.. note:: - - Although the :pipeline:`$sort` stage passes ordered documents as - input to the :pipeline:`$group` stage, :pipeline:`$group` is not - guaranteed to maintain this sort order in its own output. diff --git a/source/reference/operator/aggregation/first.txt b/source/reference/operator/aggregation/first.txt index 59d9ca7c1bc..7ebb4b45902 100644 --- a/source/reference/operator/aggregation/first.txt +++ b/source/reference/operator/aggregation/first.txt @@ -32,11 +32,10 @@ Definition Behavior -------- -When using :group:`$first` in a :pipeline:`$group` stage, the -:pipeline:`$group` stage should follow a :pipeline:`$sort` stage to -have the input documents in a defined order. - -.. include:: /includes/note-group-pipeline-sort-order.rst +When using :group:`$first` in a :pipeline:`$group` stage, the output +value depends on the order of the documents coming into pipeline. To +guarantee a defined order, the :pipeline:`$group` pipeline stage should +follow a :pipeline:`$sort` stage. Example ------- diff --git a/source/reference/operator/aggregation/last.txt b/source/reference/operator/aggregation/last.txt index 32d7455db89..80b0e42f23f 100644 --- a/source/reference/operator/aggregation/last.txt +++ b/source/reference/operator/aggregation/last.txt @@ -32,11 +32,10 @@ Definition Behavior -------- -When using :group:`$last` in a :pipeline:`$group` stage, the -:pipeline:`$group` stage should follow a :pipeline:`$sort` stage to -have the input documents in a defined order. - -.. include:: /includes/note-group-pipeline-sort-order.rst +When using :group:`$last` in a :pipeline:`$group` stage, the output +value depends on the order of the documents coming into pipeline. To +guarantee a defined order, the :pipeline:`$group` pipeline stage should +follow a :pipeline:`$sort` stage. Example ------- diff --git a/source/reference/operator/aggregation/push.txt b/source/reference/operator/aggregation/push.txt index d4a48fe648f..cc2e40fe670 100644 --- a/source/reference/operator/aggregation/push.txt +++ b/source/reference/operator/aggregation/push.txt @@ -32,6 +32,12 @@ Definition Behavior -------- +When using :group:`$push` in a :pipeline:`$group` stage, the order of +the documents in the output array is determined by the order of the +documents coming into pipeline. To guarantee a defined order, the +:pipeline:`$group` pipeline stage should follow a :pipeline:`$sort` +stage. + Memory Restrictions ~~~~~~~~~~~~~~~~~~~ @@ -67,7 +73,8 @@ compute the list of items and quantities sold for each group: db.sales.aggregate( [ - { + { $sort: { date: 1, item: 1 } }, + { $group: { _id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },