Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No matching creator found

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:

  • create aggregate helper class which encapsulates access to database using Legacy assembly.
  • implement methods which used queries constructed on bson documents
  • inject it to my async 2.0 service and replace async calls with sync

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()
like image 717
Anton Putau Avatar asked Sep 24 '15 20:09

Anton Putau


1 Answers

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.

like image 186
R J Avatar answered Sep 21 '22 20:09

R J