按嵌入式文档排序 [英] Sort by embedded document

查看:101
本文介绍了按嵌入式文档排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按嵌入文档的日期对某些文档进行排序.我的文档如下:

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")
            },
        ]
    }
]

我需要按slot.date升序对结果进行排序.因此结果应如下所示:

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")
            }
        ]
    }
]

第一个item2,因为它包含最早的slot.date(ISODate("2013-01-03T23:00:00Z")).第二个item1是因为它包含第二个最早的日期(ISODate("2013-01-18T23: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?

我尝试过的事情:

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

但是我收到语法错误.我使用MongoVUE测试查询,MongoVUE不能对嵌入式文档进行排序吗?它甚至可能是我想做什么?

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?

推荐答案

您显示的代码:

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

具有语法错误.您的代码中有两个括号,应该是:

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

.sort({slots.date : 1})

还有几种方法可以对内部子文档进行排序.也许客户端是这里最快的方法,但是,如果它证明在MongoDB本身中本机执行速度更快,您也可以使用聚合框架:

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.

这篇关于按嵌入式文档排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆