Tom Alwson
Tom Alwson

Reputation: 47

find records by element of array string

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.

Upvotes: 2

Views: 66

Answers (1)

Dĵ ΝιΓΞΗΛψΚ
Dĵ ΝιΓΞΗΛψΚ

Reputation: 5669

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

Upvotes: 1

Related Questions