Ruby MongoDB统计数据搜索查询

Ruby MongoDB统计数据搜索查询,ruby,mongodb,mongoid,moped,Ruby,Mongodb,Mongoid,Moped,我在MongoDB中有一个事件集合,其中包含一些HTTP请求信息。我想知道是否有一种方法可以根据特定的“路径”值找到响应最多的“动态”。例如,对于“random_path_users”,我希望找到“dyno”值,该值的计数数优于其他值。对于这种特定类型的请求,dyno {"_id"=>BSON::ObjectId('54738b3ff572e27e6b4b2a3b'), "method"=>"GET", "path"=>"users_count_pending_msg", "d

我在MongoDB中有一个事件集合,其中包含一些HTTP请求信息。我想知道是否有一种方法可以根据特定的“路径”值找到响应最多的“动态”。例如,对于“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}

使用聚合框架:

db.events.aggregate([
    { "$match" : { "path" : "random_path_users" } },
    { "$group" : { "_id" : "$dyno", "count" : { "$sum" : 1 } } },
    { "$sort" : { "count" : -1 } },
    { "$limit" : 1 }
])
这将返回
dyno
的值,其中
path
等于“random\u path\u users”的所有请求中请求数最大