find records by element of array string

Viewed 59

I tried to find all records in collection by specific array element(underlined in blue): enter image description here

This is my code:

    FilterDefinition<string> queryTemplate = Builders<string>.Filter.Eq(pr => pr, templateId);
    return Database.GetCollection<AutoSave>(MongoDb.AutoSaveCollection)
        .Find(Builders<AutoSave>.Filter.ElemMatch(x => x.Templates, queryTemplate)).ToList();

But it doesnt work.

1 Answers

give the following filter a try:

    var filter = Builders<AutoSave>.Filter.Where(s => s.Templates.Contains(templateId));

    var result = collection.Find(filter).ToList();

here's a test program

Related