[SERVER-8065] Add a way to determine a document's rank after a $sort Created: 03/Jan/13 Updated: 06/Dec/22 Resolved: 01/Jun/21 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Aggregation Framework |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Sam Halliday | Assignee: | Backlog - Query Optimization |
| Resolution: | Duplicate | Votes: | 8 |
| Labels: | expression | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||
| Assigned Teams: |
Query Optimization
|
||||||||||||||||
| Participants: | |||||||||||||||||
| Description |
|
It would be useful to be able to add a documents position in a sort to the document. For example, there could be an additional option to the $sort stage:
Which would output documents sorted by {a: 1, b: 1}, and inject an additional field 'index', containing it's position in the sort order (overwriting an existing field if applicable). Original DescriptionOriginally posted https://groups.google.com/d/topic/mongodb-user/PL_g1RCmPsI/discussion It would be very useful to be able to extract the indexed position of a document from a group statement, or a sort. e.g. consider the use case of building up a leader board / league table. Workaround suggestions welcome in the meantime. |
| Comments |
| Comment by Joe Kanaan [ 01/Jun/21 ] | ||||||
|
This can be achieved using $setWindowFields and $rank with a single sort field (SERVER-56572 will add support for the compound version) | ||||||
| Comment by David Percy [ 06/May/21 ] | ||||||
|
The $rank window function should solve this, but in the current implementation it doesn't allow a compound sort (SERVER-56572).
| ||||||
| Comment by Charlie Swanson [ 04/Feb/16 ] | ||||||
|
I've changed the title/description of this ticket to restrict the scope to the position in the $sort. I believe Asya's comment above describes a way to do this using $unwind with the includeArrayIndex option. However, it is still impossible to get the position in the sort, so this ticket will track that work. Please feel free to leave a comment if you disagree with this change. | ||||||
| Comment by Asya Kamsky [ 15/May/13 ] | ||||||
|
if | ||||||
| Comment by Eliot Horowitz (Inactive) [ 03/Jan/13 ] | ||||||
|
We try not to do functions that only work non-sharded, as then people get quite surprised when they loose that functionality. Will see if there is a way to implement this efficiently sharded. | ||||||
| Comment by Sam Halliday [ 03/Jan/13 ] | ||||||
|
Eliot, exactly this is not the last step in a pipeline for me (there are further constraints and limits, etc). I don't shard this collection, so that is not a concern. Happy for this to be a no-shards-allowed function. | ||||||
| Comment by Eliot Horowitz (Inactive) [ 03/Jan/13 ] | ||||||
|
This could be interesting, but also problematic. This could be very useful if the projection is not the last step. The problem is that requires consistency from all shards. | ||||||
| Comment by Sam Halliday [ 03/Jan/13 ] | ||||||
|
Hi Stephen, Not sure what the "debugging with submitter" status means. I consider MR / JS console approaches to be (hacky) workarounds. I'd still like to see a feature to insert the position of a document (in an aggregation) into the document through a group or project. Regards, |