Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-8065

Add a way to determine a document's rank after a $sort

    • Type: Icon: New Feature New Feature
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • Labels:
    • Query Optimization

      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:

      {$sort: {
          order: {a: 1, b: 1},
          includeSortIndex: 'index'
      }}
      

      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 Description

      Originally 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.

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            fommil Sam Halliday
            Votes:
            8 Vote for this issue
            Watchers:
            18 Start watching this issue

              Created:
              Updated:
              Resolved: