Is it possible to retrieve the field or fields in which a regex match was found in my query result? For example:
If I find a result in the 'facebook' field;
Let's say my req.body.key = 'crazy' and inside my database, I have 'crazy' in the 'facebook' field. I would like the output of the query to include the CitizenProfile model along with the name of the field where the result was found. In this case, the field name would be 'facebook'
Note: The query currently provides the model, but I also need the name of the field(s) where the regex match is found. Is this feasible? Thank you!
app.post('/v1/profile/search', (req, res) => {
async.waterfall([
function (next) {
CitizenProfile.find({
$or: [{'first_name': {$regex: req.body.key, $options:'i'}}, {'middle_name': {$regex: req.body.key, $options:'i'}},
{'last_name': {$regex: req.body.key, $options:'i'}}, {'email': {$regex: req.body.key, $options:'i'}},
{'facebook': {$regex: req.body.key, $options:'i'}}, {'linkedin': {$regex: req.body.key, $options:'i'}},
{'skills': {$regex: req.body.key, $options:'i'}}],
'id_citizen': {$ne: req.body.id_citizen},
'is_hidden': {$ne: true}
})
.exec(function (err, data) {
...