-
Type: Task
-
Resolution: Unresolved
-
Priority: Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: Aggregation Framework
-
Query Execution
It is rather awkward that to get topN array elements, you would do $sortArray+$slice while there's topN accumulator available.
Proposing:
$topN: {input: <expression resolving to array>, n: <n expression>, sortBy: <spec>}
e.g.:
[ { $set: { topThreeOptions: { $topN: { input: "$options", n: 3, sortBy: { votes: -1 }, }, }, fiveWatchersAZ: { $topN: { input: "$watchers", n: 5, sortBy: 1 }, }, }, }, ]
- related to
-
SERVER-59149 Add $top/$bottom/$topN/$bottomN as expressions
- Closed