DEV Community

Cover image for .Net Analyzer for MongoDB
rachelle palmer for MongoDB

Posted on

.Net Analyzer for MongoDB

C# and .NET developers know that it can sometimes be frustrating to work idiomatically with MongoDB queries and aggregations: without a way to see if your LINQ query or Builder expression corresponds to the MongoDB Query API during development, we had to wait for runtime errors in order to troubleshoot queries.

Not anymore.

With the new MongoDB Analyzer for .NET, instead of mentally mapping the idiomatic version of a query in C# to the MongoDB Query API, the MongoDB Analyzer does it for you - and even provides the generated Query API expression right in your IDE. The MongoDB Analyzer surfaces useful hints and helpful warnings on invalid expressions at compile time, bringing greater visibility to the root causes of bugs. When used together with the recently released LINQ3 provider (supported in MongoDB C#/.NET Driver 2.14.0 and higher), you can compose and understand more advanced queries easily, including aggregations. The MongoDB Analyzer can also be used from the CLI to integrate into CI/CD pipelines.

Image description

In Visual Studio, install the MongoDB.Analyzer NuGet package via package manager:

Install-Package MongoDB.Analyzer -Version 1.0.0
Enter fullscreen mode Exit fullscreen mode


dotnet add package MongoDB.Analyzer --version 1.0.0
Enter fullscreen mode Exit fullscreen mode

for the .net CLI.

Once installed, it will be added to your project’s Dependencies list, under Analyzers. Once the analyzer has run, you’ll find all of the diagnostic warnings output to the Error List panel. As you inspect your code, you’ll also see that any unsupported expressions will be highlighted.

MongoDB Analyzer surfaces any unsupported expressions and invalid queries at compile time. You’ll find all of these issues listed as warnings in the Visual Studio Error List. This is also quite useful as not all LINQ expressions are supported by the MongoDB C#/.NET driver right now. Similarly, supported expressions will differ slightly depending on which version of LINQ you use. I'm loving tinkering around with it.

Check out the docs and learn how to analyze your code here.

Top comments (1)

adriennetacke profile image
Adrienne Tacke

See the full tutorial and blog post here: