MongoDB search query for stats

63 Views Asked by At

I have an event collection in MongoDB that contains some HTTP requests informations. I would like to know if there's a way to find the "dyno" that has responded the most based on a specific "path" value. For example for "random_path_users" I would like to find the "dyno" value whose count number is superior to others dyno for this specific kind of request.

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

There are 1 best solutions below

0
On BEST ANSWER

Use the aggregation framework:

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

This will return the value of dyno with the largest count of requests among all requests with path equal to "random_path_users".