save and query mongodb collection as dynamic ExpandoObject
QUESTION:
I'm saving a dynamic object in my database but I would also like to retrieve it as a dynamic object. How can this be done? I tried it like so:
public dynamic GetItemById(ObjectId id)
{
dynamic result = Db.GetCollection("Items").Find(x => x.Id == id).FirstOrDefaultAsync().Result;
return result;
}
ANSWER:
You can use the string-based syntax, since the expression doesn't offer any advantages with dynamic anyway:
var cursor = db.GetCollection("foo").
Find(Builders.Filter.Eq("_id", someId));
or
var cursor = db.GetCollection("foo").
Find(Builders.Filter.Eq("_id", someId));
|