DotNetGoose DotNetGoose - 4 years ago 295
C# Question

MongoDb c# driver find item in array by field value

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
builder, but how can i do it with typed lambda notations ?


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 Source

There is ElemMatch

var filter = Builders<Post>.Filter.ElemMatch(x => x.Tags, x => x.Name == "test");
var res = await collection.Find(filter).ToListAsync()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download