Sorting embedded documents array?

I thought I had the syntax right for sorting an embedded documents array - but I can see now why what I was trying doesn’t work.

        { _id: objectId },
          limit: 1,
          projection: { gizmos: 1, ownerId: 1, type: 1, _id: 0 },
          sort: [['gizmos.order', 'asc']],

As you can see, what I’m hoping for is to sort the document’s embedded Gizmos document array by order. Reading more carefully, it looks like the sort findOption is only for sorting the target documents themselves - not the target documents’ embedded documents array.

From the reading I’ve done, it looks like I’m going to need to use the aggregation pipeline. Is that correct? In my dev environment, I’m on the community DB, so I think I may not be able to use aggregation functionality.

Any tips? Thanks.

Edit to add:
The sort property is also optional - in case that matters as far as tips go.

If running version 5.2 and up you might be able to $sortArray in your projection.


Thanks Steeve. I’m going to give it a try. I’m not super hopeful this will work in my dev environment, unless something has changed. The last time I checked, the free tier DB was ineligible for using the aggregation pipeline. But that was a year or more ago so I’m not sure if that’s changed. Will report back.