在我的資料庫集合中,我有兩個物件。
[
{"_id" : 1, name: "notLowercased"},
{"_id" : 2, name: "lowercased"},
]
我正在使用 find 和 $regex 來查找包含一些字串的名稱。
data = await CatalogModel.find({name: {$regex : searcher.toString().toLowerCase()}})
例如我的輸入字串是“小寫”。
結果我得到了一個陣列
[
{"_id" : 2, name: "lowercased"},
]
但我想得到這樣的結果:
[
{"_id" : 1, name: "notLowercased"},
{"_id" : 2, name: "lowercased"},
]
我知道這是因為名稱“notLowercased”不是小寫的。
如何小寫此請求中的名稱欄位?
uj5u.com熱心網友回復:
您可以添加這樣的$options引數:$options: "i".
正如檔案中所解釋的:
i:不區分大小寫以匹配大小寫。有關示例,請參閱執行不區分大小寫的正則運算式匹配。
即使你可以避免 toLowerCase()
data = await CatalogModel.find({name: {$regex : searcher.toString(), "$options": "i" }})
在這里和沒有這里的例子toLowerCase()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/354243.html
標籤:javascript MongoDB 表达 猫鼬
