AndreasKnudsen
AndreasKnudsen

Reputation: 3491

Comments on queries through MongoDB .Net driver?

Reading up on MongoDB performance troubleshooting I see that MongoDB supports appending comments on queries. We thought we might add our correlationid to queries so we can correlate which features result in slow queries and/or check whether when we discover slowness in a feature if we can spot some slowness in mongodb.

How can we add comments to queries and/or commands through the .net driver?

Upvotes: 0

Views: 186

Answers (1)

Wan B.
Wan B.

Reputation: 18845

How can we add comments to queries and/or commands through the .net driver?

You can utilise FindOptions.Comment to set a comment on a query or a command. For example:

var collection = database.GetCollection<MyObject>("collectionName");

var filter = Builders<MyObject>.Filter.Eq("Name", "Foo");

FindOptions myFindOptions = new FindOptions();
myFindOptions.Comment = "THIS IS FEATURE XYZ"; 

var cursor = collection.Find<MyObject>(filter, myFindOptions).ToList();

Once you set the database profiler i.e. via db.setProfilingLevel(), you can then query the (database).system.profile collection to find it. For example:

db.system.profile.find({
    ns:"dbName.collectionName", 
    "command.comment":"THIS IS FEATURE XYZ"
}); 

// Example result:
{   
    "op": "query",   
    "ns": "dbName.collectionName",   
    "command": {
         "find": "collectionName",
         "filter": {
             "Name": "Foo"
         },
         "comment": "THIS IS FROM FEATURE XYZ",
         "$db": "dbName",
         "lsid": {
              "id": UUID("6b722166-f50b-409c-85f0-2711633baff2"))
         }   
    },
 ....
}

The above .NET/C# code snippet is written using MongoDB .NET/C# driver v2.9.3.

Upvotes: 1

Related Questions