Reputation: 731
I'm trying to have an advanced search form which takes multiple criteria and includes them in a mongoose search, this is the approach I was thinking
router.post('/search/university', function(req, res) {
var university = req.body.university.toLowerCase();
var searchCriteria = education_key + ":" + {$RegExp("^" + university)};
User.find({searchCriteria}, function(err, matching_users){
res.render('searchresults', {user_array : matching_users, school : req.body.university});
});
});
However, I can't just construct the searchCriteria like this because apparently there are parentheses and such which don't work.
Upvotes: 1
Views: 320
Reputation: 151112
You should be manipulating this as JavaScript objects rather than trying to build strings:
router.post('/search/university', function(req, res) {
var university = req.body.university.toLowerCase();
var searchCriteria = { "education_key": {}};
searchCriteria["education_key"] = { "$regex": "^" + university };
User.find(searchCriteria, function(err, matching_users){
res.render('searchresults', {user_array : matching_users, school : req.body.university});
});
});
The $regex
operator is optional but a safe way of making sure the serialization works correctly
Upvotes: 1