Docs Menu

Analyze Your Code

On this page

  • Overview
  • Analyze Builders
  • Analyze Builders in Visual Studio
  • Analyze LINQ
  • Analyze LINQ in Visual Studio
  • Analyze LINQ3
  • Use the MongoDB Analyzer From the Command Line

Learn how to use the MongoDB Analyzer to analyze your MongoDB .NET driver code. The MongoDB Analyzer can analyze builder and Language Integrated Query (LINQ) expressions.

A builder is a class provided by the .NET driver to help you construct common operations like queries and updates.

LINQ is a query syntax included in the C# language. The .NET driver can translate a subset of LINQ expressions into MongoDB aggregation pipelines.

To learn more about builders, see Builders in the .NET driver documentation.

To learn more about LINQ, see the following resources:

  • LINQ in the .NET driver documentation
  • LINQ in the Microsoft C# guide

To learn more about aggregation pipelines, see Aggregation in the MongoDB manual.

Note
Runtime Differences

Although the MongoDB Query API translations generated by the MongoDB Analyzer have the same query shape as your runtime .NET driver queries, there may be slight differences due to the following factors:

  • Your Serialization Settings
  • Your LINQ Settings

The difference between the translations the MongoDB Analyzer generates and your queries at runtime should not impact your ability to analyze and debug your code.

To learn more about serialization and LINQ settings, see the FAQ page.

Use the MongoDB Analyzer to translate your builder expressions into the MongoDB Query API. Click the following tabs to see an example of a builder expression and its corresponding MongoDB Query API translation:

Note
Variable Names

The MongoDB Query API translations generated by the MongoDB Analyzer contain variable names from your .NET driver code. The .NET driver replaces these variable names with their corresponding values when your application communicates with MongoDB.

To analyze your builder expressions in Visual Studio, perform the following actions:

  1. Install the MongoDB Analyzer as described in the Install guide.
  2. Write a builder expression with the .NET driver
  3. Move your mouse over the ... annotation beneath the first method of your builder expression to display an information message that contains the MongoDB Query API translation.

Click the following tabs to see a builder expression with and without an information message displayed:

Tip
Error List Panel

You can view the output from the MongoDB Analyzer in the Visual Studio Error List Window.

To learn more, see Error List Window from Microsoft.

Use the MongoDB Analyzer to learn the following about your LINQ expressions:

  • How your LINQ expressions translate into the MongoDB Query API
  • If any of your LINQ expressions are not supported

Click the following tabs to see an example of a LINQ expression and its corresponding MongoDB Query API translation:

Note
Variable Names

The MongoDB Query API translations generated by the MongoDB Analyzer contain variable names from your .NET driver code. The .NET driver replaces these variable names with their corresponding values when your application communicates with MongoDB.

To analyze your LINQ expressions in Visual Studio, perform the following actions:

  1. Install the MongoDB Analyzer as described in the Install guide.
  2. Write a LINQ expression with the .NET driver.
  3. Move your mouse over the ... annotation beneath the first method of your LINQ expression to display an information message that contains the MongoDB Query API translation.

Click the following tabs to see a LINQ expression with and without an information message displayed:

If your LINQ expression is not supported, the MongoDB Analyzer outputs a NotSupportedLinqExpression warning.

Click the following tabs to see a code snippet containing an unsupported LINQ expression and the corresponding warning message displayed by the MongoDB Analyzer:

Tip
Error List Panel

You can view the output from the MongoDB Analyzer in the Visual Studio Error List Window.

To learn more, see Error List Window from Microsoft.

To view more examples of unsupported LINQ expressions, see the MongoDB Analyzer Github repository.

To analyze a LINQ3 expression, you must configure the MongoDB Analyzer to use the LINQ3 provider. To learn how to configure your LINQ provider, see the configuration guide.

Important
Expressions Supported Only by LINQ3

If your .NET driver version supports LINQ3 but you configure your MongoDB Analyzer to use the default LINQ provider (LINQ2), the MongoDB Analyzer informs you if your LINQ expression is supported by LINQ3 but not LINQ2.

Click the tabs to see a LINQ expression supported by LINQ3 but not LINQ2 and the corresponding warning output by the MongoDB Analyzer:

To learn more about LINQ3, see LINQ3 in the .NET driver documentation.

To view examples of expressions the .NET driver only supports with the LINQ3 provider, see the MongoDB Analyzer Github repository.

To run the MongoDB Analyzer from the command line and save your results to a SARIF format file, perform the following actions:

  • Install the MongoDB Analyzer as described in the Install guide.
  • Execute the following command:
dotnet build -property:ErrorLog=<Path to save your MongoDB Analyzer report>

To learn more about dotnet build, see .NET Fundamentals from Microsoft.

To learn more about the ErrorLog setting, see Error and Warning Options from Microsoft.

←  Install the MongoDB AnalyzerConfigure the MongoDB Analyzer →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.