[SERVER-16989] Add $group accumulation operators $first-n and $last-n Created: 22/Jan/15  Updated: 22/Jan/15  Resolved: 22/Jan/15

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: David Hows Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-9377 Allow collecting "top" N values for e... Closed
Related
Participants:

 Description   

We have operators $first and $last, these accumulation operators allow you to select the first or last entries for a given value within a group operator.

Can we add a $first-n and $last-n operator which would return several of the last values in an array as such:
Initial Data

for(i=0;i<100;i++){db.t2.insert({name :"one", x:i})}
for(i=0;i<100;i++){db.t2.insert({name :"two", x:i})}
for(i=0;i<100;i++){db.t2.insert({name :"three", x:i})}

Aggregation command example

db.t2.aggregate([{$group: {_id:"$name", x :{ "$last" :["$x", 3]}}}])

Example output

{ "_id" : "three", "x" : [ 99, 98, 97 ] }
{ "_id" : "two", "x" : [ 99, 98, 97 ] }
{ "_id" : "one", "x" : [ 99, 98, 97 ] }


Generated at Thu Feb 08 03:42:56 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.