MongoDB搜索统计信息的查询

时间:2014-11-24 20:18:24

标签: ruby mongodb mongoid moped

我在MongoDB中有一个包含一些HTTP请求信息的事件集合。我想知道是否有办法找到" dyno"基于特定的路径"值。例如," random_path_users"我想找到" dyno"对于这种特定类型的请求,其计数值优于其他dyno的值。

{"_id"=>BSON::ObjectId('54738b3ff572e27e6b4b2a3b'), "method"=>"GET", "path"=>"users_count_pending_msg", "dyno"=>"web.1", "connect"=>1, "service"=>10}
{"_id"=>BSON::ObjectId('54738b3ff572e27e6b4b2a3c'), "method"=>"GET", "path"=>"users_count_pending_msg", "dyno"=>"web.1", "connect"=>6, "service"=>13}
{"_id"=>BSON::ObjectId('54738b3ff572e27e6b4b2a3d'), "method"=>"GET", "path"=>"users_get_score", "dyno"=>"web.5", "connect"=>3, "service"=>155}
{"_id"=>BSON::ObjectId('54738b3ff572e27e6b4b2a3e'), "method"=>"GET", "path"=>"users_get_msg", "dyno"=>"web.10", "connect"=>1, "service"=>32}
{"_id"=>BSON::ObjectId('54738b3ff572e27e6b4b2a53'), "method"=>"POST", "path"=>"random_path_users", "dyno"=>"web.3", "connect"=>3, "service"=>55}
{"_id"=>BSON::ObjectId('54738b3ff572e27e6b4b2a54'), "method"=>"GET", "path"=>"random_path_users", "dyno"=>"web.10", "connect"=>1, "service"=>45}
{"_id"=>BSON::ObjectId('54738b3ff572e27e6b4b2a55'), "method"=>"GET", "path"=>"users_get_msg", "dyno"=>"web.10", "connect"=>2, "service"=>41}
{"_id"=>BSON::ObjectId('54738b3ff572e27e6b4b2a56'), "method"=>"POST", "path"=>"random_path_users", "dyno"=>"web.3", "connect"=>2, "service"=>31}
{"_id"=>BSON::ObjectId('54738b3ff572e27e6b4b2a57'), "method"=>"GET", "path"=>"users_count_pending_msg", "dyno"=>"web.11", "connect"=>1, "service"=>232}
{"_id"=>BSON::ObjectId('54738b3ff572e27e6b4b2a58'), "method"=>"POST", "path"=>"api_users", "dyno"=>"web.4", "connect"=>3, "service"=>37}
{"_id"=>BSON::ObjectId('54738b3ff572e27e6b4b2a59'), "method"=>"GET", "path"=>"users_count_pending_msg", "dyno"=>"web.6", "connect"=>0, "service"=>10}

1 个答案:

答案 0 :(得分:0)

使用聚合框架:

db.events.aggregate([
    { "$match" : { "path" : "random_path_users" } },
    { "$group" : { "_id" : "$dyno", "count" : { "$sum" : 1 } } },
    { "$sort" : { "count" : -1 } },
    { "$limit" : 1 }
])

这将返回dyno的值,其中path等于“random_path_users”的所有请求中的请求数最多。