简体   繁体   English

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

[英]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. 现在,我必须根据对象的'created'属性对messages数组内的对象进行排序。 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 您可以使用Mongodb Aggregation框架

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据 object 属性(即数组)对对象数组进行排序 Javascript - How to sort array of objects based on object property (which is Array) Javascript 如何对未排序的对象数组的 OBJECT 中的元素重新排序? - How can I reorder an ELEMENT inside of an OBJECT of an array of OBJECTS NOT sort? 如何通过数组内的“sortby”键对这个对象进行排序? - How can I sort this object by 'sortby' key which inside an array? 对数组内的对象进行排序 - Sort objects inside a array 如何比较两个不同对象内的 arrays 的元素并显示哪个元素属于 object 内的哪个数组 - how to compare elements of arrays inside two different objects and display which element belonged to which array that was inside the object 如何合并具有相同id的对象和嵌套在数组中的object中的相应数组对象 - How to merge the objects having same id and corresponding array objects which are nested inside the object in a array 如何对数组内的多个对象进行排序/排序? - How to sort/order multiple objects inside array? 如何对数组中的不同对象数组进行排序 - How to sort different arrays of objects inside an array 如何对对象数组进行排序以再次保存对象数组 - How to sort an array of objects which hold again an array of objects 使用对象内的对象对数组进行排序 - Sort array with Objects inside of Objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM