MongoDb c# driver find item in array by field value

Vladislav Furdak picture Vladislav Furdak · Aug 3, 2015 · Viewed 45.6k times · Source

i found the way to check is the value contains in simple array :

var filter = Builders<Post>.Filter.AnyEq(x => x.Tags, "mongodb");

But how to find a complex item with many fields by a concrete field ? I found the way to write it via the dot notation approach with BsonDocument builder, but how can i do it with typed lambda notations ?

upd

i think it some kind of

builderInst.AnyIn(p => p.ComplexCollection.Select(ml => ml.Id), mlIds)

but can't check right now, is anyone could help ?

Answer

rnofenko picture rnofenko · Aug 4, 2015

There is ElemMatch

var filter = Builders<Post>.Filter.ElemMatch(x => x.Tags, x => x.Name == "test");
var res = await collection.Find(filter).ToListAsync()