Search code examples
c#mongodbmongodb-.net-drivermongodb-csharp-2.0

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 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 ?


Solution

  • There is ElemMatch

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