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.
.find(
{ _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.