Mongodb query collection as dynamic

I am storing the object dynamic

in my database, but I would also like to get it as a dynamic object. How can this be done? I tried it like this:

public dynamic GetItemById(ObjectId id)
{
    dynamic result = Db.GetCollection<dynamic>("Items").Find(x => x.Id == id).FirstOrDefaultAsync().Result;
    return result;
}

      

But this gives me the following error:

CS1963 Expression tree cannot contain dynamic operation

I know this can be fixed by using a typed object instead of a dynamic one. But I don't want to use any typed objects because this kind defeats the whole purpose of using a NoSQL database like MongoDB (or at least imho).

How can I query my collections using Id

or any other property if using objects dynamic

?

+3


source to share


1 answer


You can use string based syntax, as the expression has no advantage anyway when used dynamic

:



var cursor = db.GetCollection<dynamic>("foo").
                Find(Builders<dynamic>.Filter.Eq("_id", someId));

      

+6


source







All Articles