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

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

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Aggregation Framework
    • Labels:
      None

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

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: