【问题标题】:sorting in mongodb aggregation according to timestamp根据时间戳在mongodb聚合中排序
【发布时间】:2018-06-29 06:30:45
【问题描述】:

我有这个收藏

{“ip”:“192.168.141.1”,“ifsIndex”:“28”,“link”:“1”,“timeStamp”:“19-01-2018 18:12:42”} {“ip”:“192.168.141.1”,“ifsIndex”:“30”,“链接”:“1”,“timeStamp”:“19-01-2018 18:12:42”} {“ip”:“192.168.141.1”,“ifsIndex”:“32”,“链接”:“1”,“timeStamp”:“19-01-2018 18:12:42”} {“ip”:“192.168.141.1”,“ifsIndex”:“29”,“链接”:“1”,“timeStamp”:“19-01-2018 18:12:42”} {“ip”:“192.168.141.1”,“ifsIndex”:“31”,“链接”:“1”,“timeStamp”:“19-01-2018 18:12:42”} {“ip”:“192.168.141.1”,“ifsIndex”:“28”,“链接”:“1”,“timeStamp”:“19-01-2018 18:14:16”} {“ip”:“192.168.141.1”,“ifsIndex”:“29”,“链接”:“1”,“timeStamp”:“19-01-2018 18:16:33”} {“ip”:“192.168.141.1”,“ifsIndex”:“32”,“链接”:“1”,“timeStamp”:“19-01-2018 18:14:09”} {“ip”:“192.168.141.1”,“ifsIndex”:“28”,“链接”:“1”,“timeStamp”:“19-01-2018 18:16:33”} {“ip”:“192.168.141.1”,“ifsIndex”:“30”,“链接”:“1”,“timeStamp”:“19-01-2018 18:14:16”} {“ip”:“192.168.141.1”,“ifsIndex”:“31”,“链接”:“1”,“timeStamp”:“19-01-2018 18:16:33”} {“ip”:“192.168.141.1”,“ifsIndex”:“29”,“链接”:“1”,“timeStamp”:“19-01-2018 18:14:16”} {“ip”:“192.168.141.1”,“ifsIndex”:“30”,“链接”:“1”,“timeStamp”:“19-01-2018 18:16:33”} {“ip”:“192.168.141.1”,“ifsIndex”:“31”,“链接”:“1”,“timeStamp”:“19-01-2018 18:14:16”} {“ip”:“192.168.141.1”,“ifsIndex”:“32”,“链接”:“1”,“timeStamp”:“19-01-2018 18:16:33”}

我想将 ip 地址和 ifsIndex 组合在一起,并显示链接和时间戳的数组,并以排序的时间戳显示结果。我试过这个

db.events.aggregate([{$group:{_id:{"ifIndex":"$ifsIndex","ip":"$ip"},status:{$push:"$link"},timeStamp:{$push:"$timeStamp"}}},{$sort:{"timeStamp":1}}])

我得到的结果不是我想要的。有些值没有按时间戳排序

{ "_id" : { "ifIndex" : "31", "ip" : "192.168.141.1" }, "status" : [ "1", "1", "1" ], "timeStamp" : [ "19-01-2018 18:12:42", "19-01-2018 18:16:33", "19-01-2018 18:14:16" ] }
{ "_id" : { "ifIndex" : "29", "ip" : "192.168.141.1" }, "status" : [ "1", "1", "1" ], "timeStamp" : [ "19-01-2018 18:12:42", "19-01-2018 18:16:33", "19-01-2018 18:14:16" ] }
{ "_id" : { "ifIndex" : "32", "ip" : "192.168.141.1" }, "status" : [ "1", "1", "1" ], "timeStamp" : [ "19-01-2018 18:12:42", "19-01-2018 18:14:09", "19-01-2018 18:16:33" ] }
{ "_id" : { "ifIndex" : "30", "ip" : "192.168.141.1" }, "status" : [ "1", "1", "1" ], "timeStamp" : [ "19-01-2018 18:12:42", "19-01-2018 18:14:16", "19-01-2018 18:16:33" ] }
{ "_id" : { "ifIndex" : "28", "ip" : "192.168.141.1" }, "status" : [ "1", "1", "1" ], "timeStamp" : [ "19-01-2018 18:12:42", "19-01-2018 18:14:16", "19-01-2018 18:16:33" ] }

ifIndex 31 和 29 的时间戳不排序,其他排序。我在这里做错了什么?

【问题讨论】:

  • 首先将时间戳从字符串更改为日期或使用 ISO 格式字符串。是否要对每个数组中的时间戳进行排序?

标签: arrays mongodb sorting aggregation-framework


【解决方案1】:

$sort 时间戳,然后执行$group 以得到$push 排序

db.events.aggregate(
    [   
        {$sort:{"timeStamp":1}},
        {$group:{_id:{"ifIndex":"$ifsIndex","ip":"$ip"},status:{$push:"$link"},timeStamp:{$push:"$timeStamp"}}}

    ]
)

结果

> db.events.aggregate( [ {$sort:{"timeStamp":1}}, {$group:{_id:{"ifIndex":"$ifsIndex","ip":"$ip"},status:{$push:"$link"},timeStamp:{$push:"$timeStamp"}}}  ])
{ "_id" : { "ifIndex" : "31", "ip" : "192.168.141.1" }, "status" : [ "1", "1", "1" ], "timeStamp" : [ "19-01-2018 18:12:42", "19-01-2018 18:14:16", "19-01-2018 18:16:33" ] }
{ "_id" : { "ifIndex" : "28", "ip" : "192.168.141.1" }, "status" : [ "1", "1", "1" ], "timeStamp" : [ "19-01-2018 18:12:42", "19-01-2018 18:14:16", "19-01-2018 18:16:33" ] }
{ "_id" : { "ifIndex" : "29", "ip" : "192.168.141.1" }, "status" : [ "1", "1", "1" ], "timeStamp" : [ "19-01-2018 18:12:42", "19-01-2018 18:14:16", "19-01-2018 18:16:33" ] }
{ "_id" : { "ifIndex" : "30", "ip" : "192.168.141.1" }, "status" : [ "1", "1", "1" ], "timeStamp" : [ "19-01-2018 18:12:42", "19-01-2018 18:14:16", "19-01-2018 18:16:33" ] }
{ "_id" : { "ifIndex" : "32", "ip" : "192.168.141.1" }, "status" : [ "1", "1", "1" ], "timeStamp" : [ "19-01-2018 18:12:42", "19-01-2018 18:14:09", "19-01-2018 18:16:33" ] }
> 

在更新操作中对数组元素进行排序但在聚合中没有排序doc

【讨论】:

    猜你喜欢
    • 2015-06-06
    • 2019-01-14
    • 2023-01-17
    • 2020-01-12
    • 1970-01-01
    • 2016-02-01
    • 2015-10-11
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多