I recently made the transition from mongosharp 1.8 to 2.0. The only problem I encountered is aggregation with date fields. Let me show you how I am building a query:
var aggregateResult = Items.Aggregate() .Group( g => new {
I used this example from the documentation .
I got the following exception: No matching creator found. I checked the generated query and executed it manually - the result was perfect. After playing the test code and comparing with mine, I find the problem in the dates. So, can someone point me to correct the syntax / query rules for dates? The generated query below proves the correctness of the request.
aggregate( [ { "$group" : { "_id" : { "Day" : { "$dayOfYear" : "$DateTime" } }, } }, { "$project" : { "Day" : "$_id.Day", "_id" : 0 } } ])
Bypass
So for everything to work, I am making the following workaround:
- Create an aggregate helper class that encapsulates database access using an obsolete assembly.
- implement methods that used queries built on bson docs.
- add it to my async 2.0 service and replace asynchronous calls with synchronization
Below is the code for collecting and executing queries
_collection = new MongoDatabase(new MongoServer( MongoServerSettings.FromUrl(connectionString)), databaseName, new MongoDatabaseSettings()).GetCollection<MyClass>("collection_name"); var pipeline = new[] { match, groupBy, project, .... }; _collection.Aggregate(new AggregateArgs { Pipeline = pipeline }).ToList()
source share