I want to make full dynamic search, when user used to type one or two string characters of book in book store UI at search box it should bring all match value from my book table that can be entire my table. The kind of object that user want to search is not important it should bring all matched values.
const searchObjects = req.query;
const result =await modelbook.find({
//this place is for query that i don't know how I should write
});
res.status(200).json({resultis: result});
})
and book model has these properties name price Url author ...
CodePudding user response:
You should use $regex
.
const search = asyncWrapper(async (req, res) => {
var { value } = req.query;
const result = await modelbook.find({
$or: [
{ name: { $regex: value, $options: "i" } },
{ price: { $eq: Number(value) ? value : 0 } },
{ url: { $regex: value, $options: "i" } },
{ description: { $regex: value, $options: "i" } },
{ author: { $regex: value, $options: "i" } }
],
});
res.status(200).json({ resultis: result });
})