-
Type: New Feature
-
Resolution: Duplicate
-
Priority: 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.
- duplicates
-
SERVER-9377 Allow collecting "top" N values for each group
- Closed