2

Using the latest version (2.14) is there any way to view the bson query document generated by a specific linq query?

I want to do this for two reasons:

  1. debugging queries
  2. copying them to run in another mongo client like compass

I know I can enable profiling, but I can't see any way to guarantee a query you find in the mongo log was generated by a specific line of code or query. Plus it's a bit long winded to do it via profiling.

2 Answers 2

3

You have 2 options to get MQL query from LINQ request:

  1. Install lately released query analyzer. enter image description here
  2. Configure CommandStartedEvent event subscriber and analyze Command document. Pay attention that you may need to remove some technical fields like $db (maybe few more) that might not be parsed by compass correctly, you will see it in the exception message if any.

UPDATE: 3. You can also use ILogger implementation. You can find details here

Sign up to request clarification or add additional context in comments.

Comments

2

@dododo answer is the right and best one, I'm just adding some code here which works for option 2:

    var settings = MongoClientSettings.FromUrl(new MongoUrl(@"mongodb://localhost"));
    settings.ClusterConfigurator = builder =>
    {
        builder.Subscribe<CommandStartedEvent>(x =>
        {
            var queryDocument = x.Command;
        });
    };

    var client =  new MongoClient(settings);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.