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.
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.
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.
MongoDB allows you to create indexes, even on nested fields in subdocuments, to keep queries performing well even as collections grow very large.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With