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

XMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Duplicate
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • None
    • None
    • 3
    • None
    • None
    • None
    • None
    • None
    • None
    • 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
              Reporter:
              David Hows (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: