mongoose search in multiple fields 1

mongoose search in multiple fields

let regex = new RegExp(value.searchQuery,'i');
const filterd = await Model.find({ $and: [ { $or: [{title: regex },{description: regex}] }, {category: value.category}, {city:value.city} ] })

Here is what the above code is Doing:
1. We are creating a new regex object with the value of the searchQuery.
2. We are using the $and operator to combine the $or operator with the category and city.
3. The $or operator is used to search for the searchQuery in the title and description.
4. The $and operator is used to combine the $or operator with the category and city.

Similar Posts