|
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 ] }
|
|