根据时间戳排序mongodb聚合

时间:2018-01-19 23:14:49

标签: arrays mongodb sorting aggregation-framework

我有这个系列

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

我想将ip地址和ifsIndex组合在一起,并显示link和timeStamp的数组,并在排序的timestamp中显示结果。我试过这个

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

我得到的结果不是我想要的。某些值未按timeStamp

排序
{ "_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的timeStamp未排序,其他排序。我在这做错了什么?

1 个答案:

答案 0 :(得分:0)

$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

中进行排序