如何对存储在对象中的数组内的对象进行排序

时间:2015-04-21 09:55:39

标签: javascript arrays mongodb sorting

我创建了一个名为“Conversation”的集合,其中一个文档如下。

{
    "_id" : ObjectId("5535fb3172163e5f1561daa7"),
    "messages" : [
        {
            "from" : ObjectId("5534b2992a104ed914435c31"),
            "_id" : ObjectId("5535fb3172163e5f1561daa8"),
            "created" : ISODate("2015-04-21T07:24:33.495Z"),
            "read" : false,
            "message" : "second first  message",
            "participants" : [
                ObjectId("5534b2992a104ed914435c31"),
                ObjectId("5530af38576214dd3553331c")
            ]
        },
        {
            "from" : ObjectId("5534b2992a104ed914435c31"),
            "_id" : ObjectId("5535fb6472163e5f1561daa9"),
            "created" : ISODate("2015-04-21T07:25:24.349Z"),
            "read" : false,
            "message" : "second second  message",
            "participants" : [
                ObjectId("5534b2992a104ed914435c31"),
                ObjectId("5530af38576214dd3553331c")
            ]
        },
        {
            "from" : ObjectId("5534b2992a104ed914435c31"),
            "_id" : ObjectId("5535fb8272163e5f1561daaa"),
            "created" : ISODate("2015-04-21T07:25:54.190Z"),
            "read" : false,
            "message" : "second third  message",
            "participants" : [
                ObjectId("5534b2992a104ed914435c31"),
                ObjectId("5530af38576214dd3553331c")
            ]
        }
    ],
    "participants" : [
        ObjectId("5534b2992a104ed914435c31"),
        ObjectId("5530af38576214dd3553331c")
    ],
    "__v" : 2
}

现在我必须根据对象的“created”属性对messages数组中的对象进行排序。我使用了以下代码。

1.     db.conversations.aggregate({$sort: {'messages.message' : -1}}).pretty()
2.     db.conversations.find({_id :ObjectId('5536023d33e52be617b8bb27')}).sort({"messages.created":1}).pretty()

但这些都没有给我正确的结果。请帮助我得到我想要的结果。感谢

1 个答案:

答案 0 :(得分:0)

您可以使用Mongodb Aggregation框架

db.conversations.aggregate([{
    '$match':{
        '_id' :ObjectId("5536023d33e52be617b8bb27")}
    },
    {'$unwind': '$messages'},
    {'$sort': { 'messages.created' : -1 }}
])

结果: -

{
    result:[
        {
            '_id' :ObjectId("5536023d33e52be617b8bb27"),
            'messages' :{
                "from" : ObjectId("5534b2992a104ed914435c31"),
                "_id" : ObjectId("5535fb8272163e5f1561daaa"),
                "created" : ISODate("2015-04-21T07:25:54.190Z"),
                "read" : false,
                "message" : "second third  message",
                "participants" : [
                    ObjectId("5534b2992a104ed914435c31"),
                    ObjectId("5530af38576214dd3553331c")
                ]
            },
            "participants" : [
                    ObjectId("5534b2992a104ed914435c31"),
                    ObjectId("5530af38576214dd3553331c")
            ],
            "__v" : 2
        }

        {
            '_id' :ObjectId("5536023d33e52be617b8bb27"),
            'messages' :{
                "from" : ObjectId("5534b2992a104ed914435c31"),
                "_id" : ObjectId("5535fb6472163e5f1561daa9"),
                "created" : ISODate("2015-04-21T07:25:24.349Z"),
                "read" : false,
                "message" : "second second  message",
                "participants" : [
                    ObjectId("5534b2992a104ed914435c31"),
                    ObjectId("5530af38576214dd3553331c")
                ]
            },
            "participants" : [
                    ObjectId("5534b2992a104ed914435c31"),
                    ObjectId("5530af38576214dd3553331c")
            ],
            "__v" : 2
        },
        {
            '_id' :ObjectId("5536023d33e52be617b8bb27"),
            'messages' :{
                "from" : ObjectId("5534b2992a104ed914435c31"),
                "_id" : ObjectId("5535fb3172163e5f1561daa8"),
                "created" : ISODate("2015-04-21T07:24:33.495Z"),
                "read" : false,
                "message" : "second first  message",
                "participants" : [
                    ObjectId("5534b2992a104ed914435c31"),
                    ObjectId("5530af38576214dd3553331c")
                ]
            },
            "participants" : [
                    ObjectId("5534b2992a104ed914435c31"),
                    ObjectId("5530af38576214dd3553331c")
            ],
            "__v" : 2
        },
    ]
}