I have a collection of documents each of which has an embedded array. I need the elements of each array to be sorted within themselves, while the containing documents should remain ordered as they are.
As an exemplification, taking the data from this similar question on sorting:
[
{
name: 'item1',
slots: [
{ date : ISODate("2013-01-18T23:00:00Z") },
{ date : ISODate("2013-02-05T23:00:00Z") },
{ date : ISODate("2013-03-24T23:00:00Z") },
]
},
{
name: 'item2',
slots: [
{ date : ISODate("2013-01-12T23:00:00Z") },
{ date : ISODate("2013-01-03T23:00:00Z") },
{ date : ISODate("2013-03-04T23:00:00Z") },
]
},
{
name: 'item3',
slots: [
{ date : ISODate("2013-03-14T23:00:00Z") },
{ date : ISODate("2013-02-18T23:00:00Z") },
{ date : ISODate("2013-03-07T23:00:00Z") },
]
}
]
My expected result is slightly different:
[
{
name: 'item3',
slots: [
{ date : ISODate("2013-02-18T23:00:00Z") },
{ date : ISODate("2013-03-07T23:00:00Z") },
{ date : ISODate("2013-03-14T23:00:00Z") },
]
},
{
name: 'item2',
slots: [
{ date : ISODate("2013-01-03T23:00:00Z") },
{ date : ISODate("2013-01-12T23:00:00Z") },
{ date : ISODate("2013-03-04T23:00:00Z") },
]
},
{
name: 'item1',
slots: [
{ date : ISODate("2013-01-18T23:00:00Z") },
{ date : ISODate("2013-02-05T23:00:00Z") },
{ date : ISODate("2013-03-24T23:00:00Z") },
]
}
]
How can I achieve this in MongoDB 3.6? I'd like to use the aggregation pipeline, if possible.
I didn't try much so far, since the only stage I can think would fit for this is the $sort, but as stated in the documentation, it will sort the outer collection.
You can use the below aggregation query.
$unwindtheslotsarray followed by sorting the docs date asc and$groupback on_idto get the sorted array.$sortonnamedesc to sort the documents.