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

Viewed 2479

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;
}
2 Answers
Related