guneysus guneysus - 8 days ago 6
C# Question

Using MongoDatabase class instead of MongoDatabaseImpl

I need to set profiling level via Mongo C# Driver.

Client.GetDatabase("test")
returns
IMongoDatabase
interface which resolves to
MongoDB.Driver.MongoDatabaseImpl
at runtime.

According to MongoDB .NET Driver API Documentation
MongoDatase
class has a
SetProfilingLevel
method which I can not cast to at runtime.

BTW I also installed Legacy Driver version 2.0.1 since documentation says that
SetProfilingLevel
method is in it.

Answer

New MongoDB driver (at least 2.3) doesn't have specific method for changing profiling level.
But you can execute any command using RunCommandAsync.

public async Task SetProfilingLevelAsync(IMongoDatabase database, int level)
{
    var command = new BsonDocumentCommand<BsonDocument>(new BsonDocument("profile", level));
    await database.RunCommandAsync(command);
}