[SERVER-18163] Documents sorted in reverse order Created: 22/Apr/15 Updated: 30/Jul/15 Resolved: 22/Apr/15 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Aggregation Framework |
| Affects Version/s: | 3.0.2 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Matt Parlane | Assignee: | David Storch |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||
| Operating System: | ALL | ||||||||||||||||
| Steps To Reproduce: |
|
||||||||||||||||
| Participants: | |||||||||||||||||
| Description |
|
The code below produces this:
But it seems to me it should be the reverse. Changing the 1 to -1 in the $sort seems to reverse it again so it's in alphabetical order. Oddly, the phenomenon is not always consistent – it seems to be consistent when run via the console, but when I run the equivalent code via either PHP or Ruby, the records are sometimes ordered CAB or ACB etc. Tested on both WT and MMAPv1. |
| Comments |
| Comment by David Storch [ 22/Apr/15 ] | |||||||||||||||||||||||
The predicate language and aggregation certainly don't make this easy. I'd say your best bet is to do the $sort after the $unwind and omit the $group stage altogether. This means that you'll only get back the f:2 array elements, but otherwise I think it's what you need.
Let's move further discussion to the MongoDB user group post linked above. The SERVER project is primarily a forum for bug reports and feature requests, so this kind of question is better suited to the user group. Best, | |||||||||||||||||||||||
| Comment by Matt Parlane [ 22/Apr/15 ] | |||||||||||||||||||||||
|
Thanks for that, and sorry for wasting your time. My problem is when the situation becomes more complex, by adding more dynamic fields. For example, if the surname field had id 2, you would have this data:
How would I then search on field 1 but sort by field 2? I've got the searching part figured out, but I'm trying to do the sorting. I'm working from Asya's article here: http://askasya.com/post/dynamicattributes and in the comments she talks about using the aggregation framework to do the sorting, and I've tried to follow her very rough instructions. Cheers, Matt | |||||||||||||||||||||||
| Comment by David Storch [ 22/Apr/15 ] | |||||||||||||||||||||||
|
Thanks for reporting this issue. It looks like the output of the aggregation command isn't sorted as you expected because the $sort stage is not the last one in the pipeline. The sorted results are fed into the $group pipeline stage, which makes no guarantees about the order in which it outputs the groups. Perhaps you mean to put the $sort stage after the $group stage, as in the shell snippet below?
I know this is a toy example, but note that in this case there is no need for the aggregation framework. You can achieve the same result with the following .find():
I am closing this ticket as Works as Designed, but feel free to re-open if you have any further concerns. Best, | |||||||||||||||||||||||
| Comment by Matt Parlane [ 22/Apr/15 ] | |||||||||||||||||||||||
|
I should mention that Asya thought it might be a bug: https://groups.google.com/d/topic/mongodb-user/HylTsQCGVd4/discussion |