Translating JSON Aggregation query to C#

Hi All! I am a MongoDB DBA supporting C# .NET developers. We are having issues with an aggregation query in that it is not coming through in the way we want it from C#. When I write it out in JSON using Studio 3T, it comes back in < 1 second and returns the expected number of documents (~16,000). When my DEV tries to do it in the MongoDB C# driver it takes over 3 minutes and returns over 360,000 docs. What is coming from the driver is not structured correctly. Here is the proper query:

db = db.getSiblingDB("Benefits");
db.getCollection("BenefitSummariesQuoting").aggregate(
	[
		{
			$match: {   
			          "EffectiveDate" : { "$lte" : ISODate("2021-11-01T04:00:00.000+0000") },
               		  "$or" : [
        			            { 
        			                "EndDate" : null
        			            }, 
        			            { 
        			                "EndDate" : { "$gte" : ISODate("2021-11-01T04:00:00.000+0000") }
        			            }
        			          ], 
        			  "$or" : [
        			            { 
        			                "State" : "MD"
        			            }, 
        			            { 
        			                "State" : "M"
        			            }
        			          ]
    	            }
		},
		{   $project:  { 
                            "CarrierPlanId" : 1.0, 
                            "HsaEligible" : 1.0, 
                            "PCPRequired" : 1.0, 
                            "PlanType" : 1.0, 
                            "ReferralRequired" : 1.0, 
                            "ProductKey" : 1.0, 
                            "CarrierId" : 1.0, 
                            "BenefitSpecifications.Benefit.Name" : 1.0, 
                            "BenefitSpecifications.Coverage.OriginalDescription" : 1.0, 
                            "BenefitSpecifications.CoverageTier2.OriginalDescription" : 1.0
                        }

		},
		{
			$sort: {
			 "EffectiveDate" : -1.0, 
			 "DateInserted" : -1.0
			}
		},
	],
	{
		allowDiskUse: true
	}
);

Does anyone have any hints or guidance for how that should be represented in C#?

Thanks!

Bill

Hi @Bill_Finch and welcome in the MongoDB Community :muscle: !

  1. Please format your code correctly by using the markdown code blocks like this one:
```
Like this!
```

All the double quotes are not real double quotes, it’s not easy to work with these & as the shell doesn’t understand them.

  1. Also, in this post, I explained how to use the “Export Pipeline to Specific Language” feature of MongoDB Compass. You can also check out the official doc to export to C#, Node, Python or Java.

Cheers,
Maxime.

2 Likes

@MaBeuLux88

Thanks very much, that is exactly what I was looking for!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.