Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

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<dynamic>("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<dynamic>("foo").
                Find(Builders<dynamic>.Filter.Eq("_id", someId));

or

var cursor = db.GetCollection<ExpandoObject>("foo").
                Find(Builders<ExpandoObject>.Filter.Eq("_id", someId));
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #save #query #mongodb #collection #dynamic #ExpandoObject
ADD COMMENT
Topic
Name
4+9 =