Allereerst kun je meerdere filters combineren met &
operator
zoals dit:
var builder = Builders<Product>.Filter;
FilterDefinition<Product> filter = builder.Empty;
filter &= builder.Eq("Color", "blue");
filter &= builder.Eq("Retailer", "adidas");
filter &= builder.Eq("Category", "men");
Vervolgens kunt u Regex
gebruiken
om de producten te filteren waarvan de namen restwoorden/alle restwoorden bevatten.
OF zoeken (de naam bevat "katoen" OF "spiderman")
var restWords = new string[] { "cotton", "spiderman" };
var orReg = new System.Text.RegularExpressions.Regex(string.Join("|", restWords));
filter &= builder.Regex("Name", BsonRegularExpression.Create(orReg));
List<Product> filteredList = products.Find(filter).ToListAsync().Result;
EN zoeken (de naam bevat "katoen" EN "spiderman")
foreach (var word in restWords)
{
filter &= builder.Regex("Name", BsonRegularExpression.Create(new System.Text.RegularExpressions.Regex(word)));
}
List<Product> filteredList = products.Find(filter).ToListAsync().Result;