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

Add $group accumulation operators $first-n and $last-n

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

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

            Assignee:
            Unassigned Unassigned
            Reporter:
            david.hows David Hows
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: