2014-11-24 72 views
0

我在MongoDB中有一个包含一些HTTP请求信息的事件集合。我想知道是否有办法找到基于特定“路径”值回应最多的“测功机”。例如,对于“random_path_users”,我希望找到“dyno”值,其计数值优于其他dyno的这种特定类型的请求。MongoDB搜索查询统计

{"_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} 

回答

0

使用聚合框架:

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

这将请求的最大数返回与path等于“random_path_users”的所有请求的值。