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

Allow sorting arrays as an aggregation operation

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

      Here's the use case.
      I need a sorted array of elements within a document:

       

      
      [
      ...
         { 
            $group: {
              "_id": "$athing",
              "anArray": {$push :"$aVal"}
            }
         },
         { $addFields : { "sortedArr" : { $sort :"$anArray" } } }, //something like this
      ...
      ]
      
      

      Now, I could sort before the grouping, but that'll only work if the data set is small. If I have a much smalled subset of the data, sorting it would be much faster.

      ...
      [
      ...
       { $sort : { "aVal" : -1 } }, //Does not work for very large data sets
       { 
         $group: {
           "_id": "$athing",
           "sortedArr": {$push :"$aVal"}
         }
       },
      ...
      ]

            Assignee:
            asya.kamsky@mongodb.com Asya Kamsky
            Reporter:
            zhadow.of.light@gmail.com Gabriel Belmonte
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: