Recently I have made migration from mongosharp 1.8 to 2.0 .The only problem I have faced is aggregation with date fields.Let me show you how I construct query :
var aggregateResult = Items.Aggregate()
.Group(
g => new {
// some fields
Day = g.DateTime.DayOfYear
},
z => new {
MyKey = z.Key
// agrregation functions
})
.Project(
d => new {
// projection for other fields
d.MyKey.Day
});
I used this example from documentation.
I got the following exception: No matching creator found.
I have checked generated query and manually executed it - result was perfect. After reproducing test code and compare to my I find problem is in dates. So, can anyone point me to correct syntax/query rules for dates? The generated query below proves that query is correct.
aggregate(
[
{
"$group" : {
"_id" : {
"Day" : {
"$dayOfYear" : "$DateTime"
}
},
}
},
{
"$project" : {
"Day" : "$_id.Day",
"_id" : 0
}
}
])
Workaround
So, to make things work I do next workaround:
Below is code to get collection and execute 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()
I encountered this error today. Similar to the person asking the question, I had an anonymous type being populated from a mongo query.
The error seems to happen when the element you're fetching does not exist in database. In this case, mongo driver seems to get confused about what "type" the resulting anonymous type should be generated as.
I changed my anonymous type to a concrete type (by declaring a class for it) and that fixed the error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With