Iam trying to sort some documents by a date of a embedded document. My documents looks like:
[
{
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")
},
]
}
]
I need the result ordered by slots.date ascending. So result should look like:
[
{
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")
},
]
},
{
name: 'item3',
slots: [
{
date : ISODate("2013-02-18T23:00:00Z")
},
{
date : ISODate("2013-03-07T23:00:00Z")
},
{
date : ISODate("2013-03-14T23:00:00Z")
}
]
}
]
First item2, becouse it contains the earliest slot.date (ISODate("2013-01-03T23:00:00Z")). Second item1 becouse it contains the 2nd earliest date(ISODate("2013-01-18T23:00:00Z")) and so on .... its also possible to sort the dates in the embedded document?
What i have tried:
.sort({{slots.date : 1}})
But i get a syntax error. I use MongoVUE to test the query, may MongoVUE cant run sorting on embedded documentens? Its that even possible what i want to do?