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

Group and select the top K elements in each group

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

      I would like the ability in the aggregation framework to group and select the top K elements in each group.

      Here is an example:

      Let's say I have documents with the following structure:

      { "_id":..., "Group": 1, "Value": 4 } { "_id":..., "Group": 2, "Value": 8 } { "_id":..., "Group": 1, "Value": 10 }

      Given X, Y and K, I would like to be able to use the aggregation framework to do the following:

      • Select all documents with the "Group" property between X and Y
      • Group by the "Group" property
      • In each group: pick only the K documents with the largest "Value" property

      Because K will be much smaller than the size of the collection itself, I would like to be able to do that in a way that is memory efficient.

      There is a new feature in 2.4 where we can efficiently sort then limit with the aggregation framework. This is basically the same thing, working on groups.

      I have no preference on which operator should enable this scenario, as long as it is possible to achieve what I just described.

            Assignee:
            Unassigned Unassigned
            Reporter:
            flavien Flavien
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: