Portable aggregation query in dotnet core

Is there a way to write portable aggregation queries in dotnet core? I have some aggregation queries written in C# using Raw BsonDocument Stages, but I cant test them without debugging the whole app. I mean, Is there a way I can write my aggregation query in C# in a way that I can copy/paste it into MongoDb Shell or Compass or VSCode extension or somewhere and test it?

For example, how can I test the below aggregation query without debugging my dotnet core app?

BsonDocument pipelineStage1 = new BsonDocument{
        "$match", new BsonDocument{
            { "companyId", "12345" }

BsonDocument pipelineStage2 = new BsonDocument{
        "$group", new BsonDocument {
			{ "_id",
				new BsonDocument
					{ "osDescription", "$osDescription" },
					{ "osVersion", "$osVersion" }
				} },
			{ "total",
				new BsonDocument("$sum", 1) }

BsonDocument[] pipeline = new BsonDocument[] { 

List<BsonDocument> pResults = myCollection.Aggregate<BsonDocument>(pipeline).ToList();

I’d like to write portable native MongoDb aggregation queries in C# like this:

      $match: { companyId: "12345" }
      $group: { _id: { osDescription: "$osDescription", osVersion: "$osVersion" }, total: { $sum: "1" } }

Then I would be able to copy/paste it into Compass, or VSCode Extension and test it


Hi, @Javier123,

Welcome to the MongoDB Community Forums.

I would generally recommend writing your aggregations and LINQ using strongly-typed POCOs. (POCO == plain old C# object) The .NET/C# Driver will do the heavy lifting for you of serializing/deserializing your POCOs into BSON on the wire. This has the additional advantage of making your code and queries type-safe and more easily refactorable.

Regardless of whether you use POCOs or BsonDocuments, there are a few ways to accomplish your goal…

First is to install the MongoDB Analyzer, which is a NuGet package. Once installed, your IDE will display a tooltip for each query, which will contain the rendered MQL. This is the same MQL that the driver will send to the server. You can also use this MQL in mongosh.

Another option is to call ToString() on the query. This will return the resulting MQL.

var query = myCollection.Aggregate<BsonDocument>(pipeline);


My approach differs from the one already presented.

I keep my queries in the native mongosh/JS format inside a file. At run time, outside mongosh, in Java in particular, I read the file and get back a org.bson.Document. See Java query from shell script - #2 by steevej for more details.

I am pretty sure BsonDocument has a parse method to do the same.

Thanks, that is similar to what I was doing. So I have a set of *.mongodb files with native mongodb aggregation queries that I can easily test from MongoDb Compass or MongoDb Shell or MongoDb VSCODE extension, then I can use MongoDb Compass to convert them to C# code.

But I like the LINQ approach, although it is not portable as I wish since I need to debug the whole dotnet core microservice in order to test the query

1 Like