MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

How to enable profiling? - C# Driver

Is it possible with the MongoDB C# Driver ( v2.10.2 ) to enable profiling along with the ability to query the results?

Essentially, i’m trying to mimic the equivalent of db.setProfilingLevel(2) & db.system.profile.find().pretty() from the mongo shell.

Hi @Michael_Fyffe,

Yes, you can run database commands using IMongoDatabase.RunCommand<TResult>, or IMongoDatabase.RunCommandAsync<TResult>.

db.setProfilingLevel() is a wrapper on mongo shell for the profile command. So, in this case you can execute the profile command. For example:

var profileCommand = new BsonDocument("profile", 2);
var result = database.RunCommand<BsonDocument>(profileCommand);
Console.WriteLine(result);

You can then just query the collection system.profile:

var collection = database.GetCollection<BsonDocument>("system.profile");
var doc = collection.Find(new BsonDocument()).FirstOrDefault();

See also Database Profiler Output for more information.

Regards,
Wan.

2 Likes

Hey @wan,

That is awesome. Thank you soo much!

Is there a good way to correlate ( using the C# driver ) a query being run with a specific entry in the system.profiles collection when i query it?

Hi @Michael_Fyffe,

Try adding $comment in your queries, so that you can easily interpret and trace the profile log. For example, using .NET/C# driver you could utilise FindOperation.Comment property.

Regards,
Wan.

2 Likes

Hi @wan,

I can’t seem to find any examples of how to get or create a FindOperation.
My queries look similar to this.:

 IAsyncCursor<UserData> findCursor = await collection.FindAsync(filter, options);

Foutunately, I can see that I can specify a comment within the FindOptionsBase.Comment property.
However, how can I do the same when doing an update command? For example:

await collection.UpdateOneAsync(filter, updateDefinition, options);

Here, the UpdateOptions class doesn’t have any comment property.
Can this just not be done?

Hi @Jan_Philip_Tsanas, welcome!

Yes, for example:

FindOptions MyFindOptions = new FindOptions();
MyFindOptions.Comment = "token-001";
var cursor = collection.Find<BsonDocument>(new BsonDocument(), MyFindOptions);

Then you should be able to query it using the following:

collection = database.GetCollection<BsonDocument>("system.profile");
var filter = new BsonDocument{{"command.comment", "token-001"}};
var entries = collection.Find(filter);

Currently there is an open ticket for all MongoDB drivers to support this DRIVERS-742. In the mean time for update operations, you can attach $comment query operator on the query predicate. See more info on $comment behaviour. For example:

var updateOp = new BsonDocument{
                    {"$set", new BsonDocument{{"foo", "new"}} }};
var filter =  new BsonDocument{
                    {"foo", "old"}, {"$comment", "token-002"}};
var result = collection.UpdateOne(filter, updateOp);

Then you should be able to query it using the following:

collection = database.GetCollection<BsonDocument>("system.profile");
var filter = new BsonDocument{{"command.q.$comment", "token-002"}};
var entries = collection.Find(filter);

The above snippet was executed with MongoDB .NET/C# v2.10.2 and MongoDB server v4.2.5.

Regards,
Wan.

3 Likes