如何使用mongodb获得前五个最快的地面速度

时间:2016-10-19 14:07:45

标签: mongodb sorting mongodb-query aggregation-framework mongodb-aggregation

所以我从excel导入了这些数据,我希望能够找到前五个最快的速度。我尝试使用聚合函数,但返回的是0。

db.planes.aggregate({
    $group : {
        _id : "$msg_dummy",
        fastest : {$max : "$ground_speed"}
    }
})


{ "_id" : "MSG", "fastest" : "" }

只有一个打印出来。我需要前五名。这是一些数据

{ "_id" : ObjectId("58076dffa85331269df16994"), "msg_dummy" : "MSG", "msg_type" : 4, "flight_id" : 680, "aircraft_id" : "A1C8EB", "date" : 20131213, "time" : 214224, "latitude" : "", "longitude" : 306, "altitude" : 133, "ground_speed" : -512, "heading" : "", "dummy1" : "" }
{ "_id" : ObjectId("58076dffa85331269df16995"), "msg_dummy" : "MSG", "msg_type" : 4, "flight_id" : 680, "aircraft_id" : "A1C8EB", "date" : 20131213, "time" : 214223, "latitude" : "", "longitude" : 307, "altitude" : 133, "ground_speed" : -512, "heading" : "", "dummy1" : "" }
{ "_id" : ObjectId("58076dffa85331269df16996"), "msg_dummy" : "MSG", "msg_type" : 1, "flight_id" : 680, "aircraft_id" : "A1C8EB", "date" : 20131213, "time" : 214223, "latitude" : "", "longitude" : "", "altitude" : "", "ground_speed" : "" }
{ "_id" : ObjectId("58076dffa85331269df16997"), "msg_dummy" : "MSG", "msg_type" : 4, "flight_id" : 680, "aircraft_id" : "A1C8EB", "date" : 20131213, "time" : 214223, "latitude" : "", "longitude" : 307, "altitude" : 133, "ground_speed" : -512, "heading" : "", "dummy1" : "" }
{ "_id" : ObjectId("58076dffa85331269df16998"), "msg_dummy" : "MSG", "msg_type" : 1, "flight_id" : "AAL1538", "aircraft_id" : "ABFEFD", "date" : 20131213, "time" : 214224, "latitude" : "", "longitude" : "", "altitude" : "", "ground_speed" : "" }
{ "_id" : ObjectId("58076dffa85331269df16999"), "msg_dummy" : "MSG", "msg_type" : 4, "flight_id" : "AAL1538", "aircraft_id" : "ABFEFD", "date" : 20131213, "time" : 214224, "latitude" : "", "longitude" : 298, "altitude" : 123, "ground_speed" : 64, "heading" : "", "dummy1" : "" }
{ "_id" : ObjectId("58076dffa85331269df1699a"), "msg_dummy" : "MSG", "msg_type" : 4, "flight_id" : 680, "aircraft_id" : "A1C8EB", "date" : 20131213, "time" : 214224, "latitude" : "", "longitude" : 306, "altitude" : 133, "ground_speed" : -512, "heading" : "", "dummy1" : "" }
{ "_id" : ObjectId("58076dffa85331269df1699b"), "msg_dummy" : "MSG", "msg_type" : 1, "flight_id" : "NKS355", "aircraft_id" : "A67CA6", "date" : 20131213, "time" : 214225, "latitude" : "", "longitude" : "", "altitude" : "", "ground_speed" : "" }
{ "_id" : ObjectId("58076dffa85331269df1699c"), "msg_dummy" : "MSG", "msg_type" : 3, "flight_id" : 680, "aircraft_id" : "A1C8EB", "date" : 20131213, "time" : 214225, "latitude" : 28.64559, "longitude" : -81.57871, "altitude" : 10100, "ground_speed" : 306, "heading" : 133, "dummy1" : -512, "dummy2" : "", "field13" : "" }
{ "_id" : ObjectId("58076dffa85331269df1699d"), "msg_dummy" : "MSG", "msg_type" : 3, "flight_id" : 680, "aircraft_id" : "A1C8EB", "date" : 20131213, "time" : 214225, "latitude" : 28.64517, "longitude" : -81.57823, "altitude" : 10100, "ground_speed" : 306, "heading" : 133, "dummy1" : -512, "dummy2" : "", "field13" : "" }
{ "_id" : ObjectId("58076dffa85331269df1699e"), "msg_dummy" : "MSG", "msg_type" : 4, "flight_id" : "AAL1538", "aircraft_id" : "ABFEFD", "date" : 20131213, "time" : 214225, "latitude" : "", "longitude" : 298, "altitude" : 123, "ground_speed" : 64, "heading" : "", "dummy1" : "" }

2 个答案:

答案 0 :(得分:1)

由于ground_speed字段混合了数字和字符串值(空字符串), $max 运算符会返回最大值,因为它使用值和类型比较specified BSON comparison order表示不同类型的值。

您需要过滤文档,并仅比较ground_speed的数值:

db.planes.aggregate([
    { "$match": { 
        "ground_speed": { "$exists": true, "$type": 1 }  
    } },
    {
        "$group" : {
            "_id" : "$msg_dummy",
            "fastest" : { "$max" : "$ground_speed"}
        }
    }
])

要回答您的问题,要获得前5个速度,需要使用 $sort 管道在ground_speed字段下订购文档,创建一个{{1}的数组使用累加器运算符 $group $push 管道中的值,然后使用 {{3>从数组返回前5位管道和 $project 运算符。

以下示例显示了这一点:

ground_speed

对于不支持 $slice 运算符的MongoDB版本,作为替代方案,您可以通过限制进入 {{3}的文档数量来获得前5名使用 $slice 运算符的管道,这应该放在 $group 管道之后(在已订购的文档上):

db.planes.aggregate([
    { "$match": { 
        "ground_speed": { "$exists": true, "$type": 1 }  
    } },
    { "$sort": { "ground_speed": -1 } },
    {
        "$group" : {
            "_id" : "$msg_dummy",
            "ground_speeds" : { "$push" : "$ground_speed" }
        }
    },
    {
        "$project": {
            "_id": 0,
            "msg_dummy": "$_id",
            "top_five_fastest": { "$slice": ["$ground_speeds", 5] },
            "ground_speeds": 1
        }
    }
])

答案 1 :(得分:0)

您可以在不使用聚合查询的情况下执行此操作。

尝试以下方法:

 db.planes.find({},{_id:1,ground_speed:1}).sort({ground_speed:-1}).limit(5)