Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does MongoDb C# driver works with LINQ and dynamic documents?

I am wondering if I can even use LINQ on mongo documents that are not mapped and are fully dynamic. Reason for this is that I have ITEM aggrigator that holds unknown keys and values. This is only reason why I picked no-sql solution in the first place because EAV+SQL seems slow and overkill.

I inserted over 1m documents into my test collection that has one to ten fields with random data in it. Now I'm trying to get it back and using native Mongo queries it works, of course. Not so much using LINQ. Am I doing something wrong here?

Error

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Input string was not in a correct format.

Code

private static List<BsonDocument> Get(string query)
{
    var client = new MongoClient("mongodb://localhost");
    var server = client.GetServer();
    var db = server.GetDatabase("test");
    var collection = db.GetCollection("items");
    var collectionQuery = collection.AsQueryable();
    
    var result = collection.Find(Query.EQ("Six", 7962)).ToList(); // THIS WORKS
    //var result = collection.AsQueryable().Where(x => x["Six"] == 7962).ToList(); // ERROR

    return result;
}
like image 664
Stan Avatar asked Oct 20 '22 18:10

Stan


1 Answers

The driver does not currently support dynamic types. Here's the jira ticket for it: https://jira.mongodb.org/browse/CSHARP-539

There's also an an interesting post about some possible workaround: http://roysvork.wordpress.com/2013/04/22/using-linq-to-query-loosely-typed-data-in-mongodb/

like image 185
i3arnon Avatar answered Oct 23 '22 09:10

i3arnon