Recently, I came across a situation where I have a model of a Person with a virtual field called full_name
. This virtual field combines the first name, middle names, and last name of an individual. It proves to be very helpful when I need to search for a person based on their complete name. However, I've heard that querying on virtual fields is not possible. Is this information correct? If so, why is it not allowed? In case it's true, what would be the best approach to tackle this kind of scenario:
router.get("/:searchTerm", (req, res) => {
const st = req.params.searchTerm;
Person.find({full_name: {$regex: st, $options: "i"}}, (err, ppl) => {
res.json(ppl);
}).limit(30);
});
I appreciate any insights or guidance you can provide regarding this matter. Thank you!