我目前正在為電子商務網站使用 MERN 堆疊
我在貓鼬中找到了一種語法,例如:
Product.find({ brands: {$in : ["Brand1", "brand2", "BRAND3"], $option: 'match all cases'}})
我希望結果將匹配所有具有品牌 1 或品牌 1 或品牌 1、品牌 2 或品牌 2 的產品,依此類推......
如何在 Mongoose v6.0.13 中解決它?
uj5u.com熱心網友回復:
如果要在 mongoDB 指南針中執行,請使用以下查詢
Product.find({brands :{$in:[new RegExp('brand1', 'i'), new RegExp('brand2', 'i')]}} )
如果使用貓鼬使用
Product.find({ brands:{$in:[{ $regex: new RegExp('brand1', "i") }, { $regex: new RegExp('brand2', "i") }] })
uj5u.com熱心網友回復:
您應該在引號中傳遞品牌名稱。
Product.find({ brands: {$in : ["Brand1", "brand2", "BRAND3"], $option: 'match all cases'}})
進行不區分大小寫的搜索,
var brands = [/^Brand1$/i,/^brand2$/i,/^BRAND3$/i];
Product.find({ brands: {$in : brands}})
uj5u.com熱心網友回復:
我找到了一個解決方案:
讓正則運算式 =brands.join("|"); Product.find({ 品牌: { $regex: 正則運算式, $options: 'i' });
謝謝大家的幫助
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/503962.html
