我正在构建一个允许用户使用侧边栏过滤结果的网站。他们选择的标准越多,搜索结果就应该越具体(见附图)。
用户可以选择与他们要查找的内容相匹配的过滤器(复选框)。我为此使用 MongoDB。我的架构如下:
brandName: {
type: String,
required: false
},
productType: {
type: String,
required: false
},
skincareConcern: {
type: Array,
required: false
},
ingredients: {
type: Array,
required: false
},
skinType: {
type: Array,
required: false
}
现在,我正在使用.find()
与$or
查询但这不正确,即因为$or
返回任何这些条件的所有匹配项。我需要结果来过滤/缩小我包含的更多标准。
请参阅下面我当前的代码:
Products.find({
$or: [
{ brandName : { $in : brands, $exists: true, $ne: [] } },
{ skincareConcern : { $in : skincareConcerns, $exists: true, $ne: [] } },
{ productType : { $in : productTypes, $exists: true, $ne: [] } },
{ ingredients : { $in : ingredients, $exists: true, $ne: [] } },
{ skinTypes : { $in : skinTypes, $exists: true, $ne: [] } }
]
}
示例有效负载:
{"brands":["ACWELL","BOTANIC FARM"],"skincareConcerns":[],"productTypes":["essence","moisturizer"],"ingredients":[],"skinType":[]}