[DOCS-1669] aggregation $group reverses previous $sort Created: 02/Jul/13  Updated: 25/Aug/19  Resolved: 02/Jul/13

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Oleksii Lomako Assignee: Sam Kleinman (Inactive)
Resolution: Done Votes: 0
Labels: aggregation, group, sort
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

win7 64bit


Attachments: File test.json    
Participants:
Days since reply: 4 years, 24 weeks, 4 days ago

 Description   

aggregation $group operation reverses previous $sort operation.

this must be fixed or documented in $group and $sort that $sort will be broken /reverted if it was used before $group



 Comments   
Comment by Hassan Faghihi [ 25/Aug/19 ]

Just curious, and unrelated...

what's `u` in that json? is it valid? or it used in specific language? 
https://jira.mongodb.org/browse/DOCS-1669?focusedCommentId=372477&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-372477

Comment by Billy Tetrud [ 24/Jun/16 ]

Why does $group eff up the sort? This seems unnecessarily confusing to me.

Comment by Sam Kleinman (Inactive) [ 02/Jul/13 ]

The first $sort can use an index if there are no $project, $unwind or $group pipeline stages that proceed the $sort.

Comment by auto [ 02/Jul/13 ]

Author:

{u'username': u'tychoish', u'name': u'Sam Kleinman', u'email': u'samk@10gen.com'}

Message: DOCS-1669: adding note about ordering of group output.
Branch: master
https://github.com/mongodb/docs/commit/06bdb71347cdc3c8d9b8891ed2efee1c55221792

Comment by auto [ 02/Jul/13 ]

Author:

{u'username': u'tychoish', u'name': u'Sam Kleinman', u'email': u'samk@10gen.com'}

Message: DOCS-1669: adding note about ordering of group output.
Branch: v2.2
https://github.com/mongodb/docs/commit/9bd0ef54649350431274c0e2b8281e04d1a79abc

Comment by Oleksii Lomako [ 02/Jul/13 ]

Am i right that we cannot rely on such sorting "invertion" and must use $sort second time after $group?
the pipeline will be then:
$sort -> $group -> $sort

does first $sort make any optimization (we assume that we have index on keys)?

Comment by Eliot Horowitz (Inactive) [ 02/Jul/13 ]

This is definitely as expected.

The output of group is by definition not-ordered, that should be clear in the docs.

Generated at Thu Feb 08 07:41:36 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.