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

Add an expression to sort an array

    • Fully Compatible
    • QE 2021-11-15

      Syntax:

      {$project:{orderedArray:{$sort:{path:<array-returning-expression>[, sortBy:<someFieldInArray>, <ascending-vs-descending-option>]}}
      

      Examples it should work on:

      Input: {a: [ 1, 10, 5, 3 ]}
      {newA:{$sort:{path:"$a"}}}
      returns:  { newA: [ 1, 3, 5, 10 ] }
      
      Input: { a: [ { x:1, y:99 }, { x: 9, y: 4 } ] }
      {newA: {$sort:{path:"$a", sortBy: "$a.y" }}}
      returns: { newA: [ { x: 9, y: 4 }, { x:1, y:99 } ] }
      

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            asya.kamsky@mongodb.com Asya Kamsky
            Votes:
            5 Vote for this issue
            Watchers:
            24 Start watching this issue

              Created:
              Updated:
              Resolved: