The data structure that client gives is dynamic with few common fields.
An example is shown below.
"Publish": "some uri",
I am storing this data directly usng Bson documents as we don't have a
We wanted to have linq support for such a collection but as mongo driver
doesn't support dynamic I can't write something like
var query = from dynamic p in collection
where p.Id== id
To solve this I was using mongo query builder to write my queries which
worked but is a little bit cumbersome.
Now my approach is to get the IQueryable using
var queryable = new MongoQueryProvider(collection);
return new MongoQueryable<T>(provider);
Create linq query
var query = from p in queryable
where p.Id== Id
To Execute this query I convert this query into mongo query
var mongoquery = ((MongoQueryable<T>)query).GetMongoQuery();
Then I can get the cursor using get cursor method
private static MongoCursor GetCursor(MongoCollection collection, IMongoQueryquery)
var cursor = MongoCursor.Create(typeof(BsonDocument), collection, query);
Now I can itierate the cursor
var result = cursor.Cast<BsonDocument>().ToList();