[英]C# MongoDB check if array contains strings case insensitive
我是 MongoDB 的新手,我正在嘗試檢查輸入的字符串數組是否具有與任何文檔上的嵌套字符串數組匹配的任何值。
public IEnumerable<Cocktail> FindByIngredients(List<string> inputIngredients)
{
inputIngredients = inputIngredients.ConvertAll(ingredient => ingredient.ToLower());
var filter = Builders<Cocktail>.Filter
.AnyIn(x => x.IngredientList, inputIngredients);
var cocktails = _collection.Find(filter).ToList();
return cocktails;
}
目前,我得到了以上,但它不區分大小寫,我將如何 go 使這種大小寫不敏感?
您可以在執行查詢時指定排序規則,從而更改 MongoDB 處理字符串的方式。 有關詳細信息,請參閱此 [鏈接][1]。
例如,如果要使用 English-US 作為 locale 進行查詢,則可以使用以下代碼:
public IEnumerable<Cocktail> FindByIngredients(List<string> inputIngredients)
{
inputIngredients = inputIngredients.ConvertAll(ingredient => ingredient.ToLower());
var filter = Builders<Cocktail>.Filter
.AnyIn(x => x.IngredientList, inputIngredients);
var options = new FindOptions()
{
Collation = new Collation("en_US")
};
var cocktails = _collection.Find(filter, options).ToList();
return cocktails;
}
如果不指定排序規則,則使用簡單的二進制比較,因此區分大小寫。
如果要支持帶有索引的查詢,請記住索引還必須使用與查詢時使用的排序規則相同的排序規則。 [1]: https://www.mongodb.com/docs/manual/reference/collation/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.