Sorting nested tables

So I am trying to sort a nested array which contains an array with many variables that I want to sort by.
I recreated my issue in the following playground:
Mongo playground
As you can see I included $sort however the result I am getting is not being sorted. Any idea how can I make this work?

If you look at the $sort documentation as you use it, you will see that it is a pipeline stage that sorts the documents rather than the element of an array.

To sort elements of an array you have $sortArray.

If sorting the elements of an array is a major use-case, you might be better off sorting the elements while building the array using the $sort modifier.

I usually prefer to do this type of data cosmetic (sorting an inner table) in the application presentation layer as to reduce the amount of work done on the data server as it is naturally distributed on the workstation of the users.

As for the field “Supplier Manager”, why don’t you use a real array rather than a formatted string? You would not need $regexMatch.

1 Like