Search code examples
mongodbprofiler

Is there an equivalent tool like sql profiler for mongodb?


I would like to know if there is an equivelent tool like sql profiler for mongodb. Specifically, I would like to see what monogdb queries are being generated and are being run from my code.

So, when I have code such as:

    var Logs = MvcApplication.MongoLoggingDatabase.GetCollection<Log>("Log")
                .Find(queryDocument)
                .Select(x => new LogDto { ModelNumber = x.Request.ModelNumber, Make = x.Request.Make, TimeStamp = x.TimeStamp, UserId = x.UserId })
                .OrderByDescending(x => x.TimeStamp)
                .Skip(pageSize * (page - 1))
                .Take(pageSize);

I would like to know what actual mongodb query is being generated and ran in order to help optimize my code when it queries the database.


Solution

  • MongoDB includes a simple profiler. See here: http://www.mongodb.org/display/DOCS/Database+Profiler

    If you set the profiling level to 2, then all queries will be written to the "system.profiler" collection so you can take a look. If you set the profiling level to 1, then just the slow queries will be written (by default these are defined as queries slower than 100ms, but this a configurable parameter).

    For diagnosing slow queries, the "explain" functionality is also very helpful. See here http://www.mongodb.org/display/DOCS/Explain

    One you know which queries are slow, you can use explain to figure out which index the database is using (or not using).