Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profiling MongoDB queries when using C# driver

Is there a way to log the actual queries that are produced by the MongoDB C# driver and sent to the mongodb? Like in SQL Server, you have SQL Profiler that shows you all the incoming queries.

like image 783
Andrey Avatar asked Jun 08 '11 19:06

Andrey


People also ask

How do I enable MongoDB profiling?

Enabling and Configuring MongoDB Profiler The MongoDB profiler is enabled from mongosh or through a driver using the profile command. To enable profiling and set the profiling level, type db. setProfilingLevel() helper in mongosh where you will pass the profiling level you want as a parameter.

What is profiling in MongoDB?

The database profiler collects detailed information about Database Commands executed against a running mongod instance. This includes CRUD operations as well as configuration and administration commands. The profiler writes all the data it collects to a system.

Can we write queries in MongoDB?

MongoDB allows you to create indexes, even on nested fields in subdocuments, to keep queries performing well even as collections grow very large.


1 Answers

You can enable profiling and see actual queries in mongodb log as @pingw33n suggested.

Or you can create extention method for collection.Find and log data there:

public static class MongodbExtentions
{
    public static MongoCursor<T> FindAsAndLogQuery<T>(this MongoCollection<T> coll, 
                                                                    IMongoQuery query)
    {
        var queryString = query.ToJson();
        //log query here , insert into mongodb, etc ...
        return coll.FindAs<T>(query);
    }
}
like image 113
Andrew Orsich Avatar answered Sep 22 '22 16:09

Andrew Orsich