How to enable profiling? - C# Driver

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