node.js - Mongoose: How to create specific field criteria in a variable -



node.js - Mongoose: How to create specific field criteria in a variable -

i'm trying have advanced search form takes multiple criteria , includes them in mongoose search, approach 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, can't build searchcriteria because apparently there parentheses , such don't work.

you should manipulating javascript objects rather trying 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 optional safe way of making sure serialization works correctly

node.js mongodb express mongoose

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -