const filterCriteria = {
carColor: req.query.carColor,
languages: { $regex: `${req.query.languages}`, $options: 'i' },
incomeMonth: { $gte: req.query.incomeMonth },
incomeYear: { $gte: req.query.incomeYear },
age: { $gte: query.fromAge, $lt: query.toAge }, // age from - to
familyStatus: query.familyStatus
}
const filteredProfiles = await Profile.find(filterCriteria)
.sort({ createdAt: 1 })
.limit(100)
return res.json({ success: true, count: filteredProfiles?.length, profiles: filteredProfiles })
For instance, if I have multiple queries and one of them is related to language with the key req.query.language. I only want to apply the $regex method for this specific query key. If any query key is empty, I prefer not to include it in the find() method.