Sort by embedded document

2019-07-20 09:32发布

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?

1条回答
SAY GOODBYE
2楼-- · 2019-07-20 10:21

The code you showed:

.sort({{slots.date : 1}})

Has a syntax error. You have two brackets in your code, it should be:

.sort({slots.date : 1})

Also there are a couple of ways to sort your inner subdocuments. Maybe client side is the fastest method here however, you can also use the aggregation framework if it proves that doing it natively within MongoDB itself is faster:

db.col.aggregate([
    {$unwind: '$slots'},
    {$sort: {slots.date:1}},
    {$group: {_id: '$_id', slots: {$push: '$slots'}}
])

Something like that will sort subdocuments.

查看更多
登录 后发表回答