I'm building a site that allows users to filter results using a sidebar. The more criteria they select, the more specific their search results should be (see attached image).
Users can select filters (checkboxes) that match what they're looking for. I'm using MongoDB for this. My schema is as follows:
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
}
Right now, I'm using a .find()
with an $or
query but this isn't correct, namely because the $or
returns all the matches for any of these criteria. I need the results to filter/narrow down the more criteria I include.
See my current code below:
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: [] } }
]
}
Sample payload:
{"brands":["ACWELL","BOTANIC FARM"],"skincareConcerns":[],"productTypes":["essence","moisturizer"],"ingredients":[],"skinType":[]}
See Question&Answers more detail:os