Skip to Main Content

MongoByte MongoDB Logo

Welcome to the new MongoDB Feedback Portal!

{Improvement: "Your idea"}
We’ve upgraded our system to better capture and act on your feedback.
Your feedback is meaningful and helps us build better products.

Status Completed
Categories Database
Created by Guest
Created on Dec 18, 2021

Add $sort as an array operator in the agg framework

$filter and $map and $reduce allowed us to stop using $unwind all the time. I know $sort is likely the most computationally expensive peer function to $filter and $map but there are so many times that I have a field with an array of things (scalars or objects) and I need either the whole array in a sorted order or the first or last thing in the sort, for which I would use $arrayElemAt:[ {$sort: {input: ... }}, 0 ] . I see the $sort function signature looking almost like $filter: {$sort: {input: , as: "gg" , // name of local list, fields: [ {"$$gg.fld1":1}, {"$$gg.fld2":-1}, ... ] }
  • ADMIN RESPONSE
    Aug 1, 2025
    feel tree to add comments if $sortArray ( https://docs.mongodb.com/v5.2/reference/operator/aggregation/sortArray/ ) doesn't meet your expectations
  • Attach files
  • Guest
    Feb 25, 2022
    version 5.2 (Atlas Rapid release) includes $sortArray expression https://docs.mongodb.com/v5.2/reference/operator/aggregation/sortArray/ This will be also available in the LTS 6.0 release