Skip to content

DOCS-13063 Clarify $group order behaviour #5215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions source/includes/note-group-pipeline-sort-order.rst

This file was deleted.

9 changes: 4 additions & 5 deletions source/reference/operator/aggregation/first.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
9 changes: 4 additions & 5 deletions source/reference/operator/aggregation/last.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
9 changes: 8 additions & 1 deletion source/reference/operator/aggregation/push.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -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" } },
Expand Down