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 eba3af53309..c91dcb7fe36 100644 --- a/source/reference/operator/aggregation/first.txt +++ b/source/reference/operator/aggregation/first.txt @@ -43,11 +43,10 @@ For more information on expressions, see :ref:`aggregation-expressions`. 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 bbe2c2f78c7..255a8a076c5 100644 --- a/source/reference/operator/aggregation/last.txt +++ b/source/reference/operator/aggregation/last.txt @@ -43,11 +43,10 @@ For more information on expressions, see :ref:`aggregation-expressions`. 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 1488c00d787..2005cc3dd28 100644 --- a/source/reference/operator/aggregation/push.txt +++ b/source/reference/operator/aggregation/push.txt @@ -29,6 +29,15 @@ Definition For more information on expressions, see :ref:`aggregation-expressions`. +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. + Example ------- @@ -52,7 +61,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" } },