简体   繁体   中英

How to sort objects inside an array which is stored in an object

I have created a collection called "Conversation" and its one of the document is as follows.

{
    "_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
}

Now I have to sort the objects inside the messages array , according to the 'created' property of the object. I have used the following code.

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

But none of these giving me the right result. Please help me to get the results what I wanted. Thanks

You can use Mongodb Aggregation framework

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

Result:-

{
    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
        },
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM