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

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

    Details

      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:

      {$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.

        Activity

        Hide
        fommil Sam Halliday added a comment -

        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,
        Sam

        Show
        fommil Sam Halliday added a comment - 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, Sam
        Hide
        eliot Eliot Horowitz added a comment -

        This could be interesting, but also problematic.
        If that projection is the last step in a pipeline, then I'm not sure how much easier it is since you have the position in the client.

        This could be very useful if the projection is not the last step.
        i.e. do a sort, and the position, then re-sort based on something else.
        Then you can see people sorted by name, with their position.

        The problem is that requires consistency from all shards.
        So it would be a scaling bottleneck.

        Show
        eliot Eliot Horowitz added a comment - This could be interesting, but also problematic. If that projection is the last step in a pipeline, then I'm not sure how much easier it is since you have the position in the client. This could be very useful if the projection is not the last step. i.e. do a sort, and the position, then re-sort based on something else. Then you can see people sorted by name, with their position. The problem is that requires consistency from all shards. So it would be a scaling bottleneck.
        Hide
        fommil Sam Halliday added a comment -

        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.

        Show
        fommil Sam Halliday added a comment - 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.
        Hide
        eliot Eliot Horowitz added a comment -

        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.

        Show
        eliot Eliot Horowitz added a comment - 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.
        Hide
        asya Asya Kamsky added a comment -

        if SERVER-4588 was implemented that might be a workaround for this. Since you can basically group on a constant and build up an array with all documents (or at least known fields of interest) and then unwind with index if SERVER-4588 exists.

        Show
        asya Asya Kamsky added a comment - if SERVER-4588 was implemented that might be a workaround for this. Since you can basically group on a constant and build up an array with all documents (or at least known fields of interest) and then unwind with index if SERVER-4588 exists.
        Hide
        charlie.swanson Charlie Swanson added a comment -

        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.

        Show
        charlie.swanson Charlie Swanson added a comment - 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.

          People

          • Votes:
            2 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

            • Created:
              Updated:
              Days since reply:
              43 weeks, 2 days ago
              Date of 1st Reply: