问题
I'm trying to do like search into mongodb with Javascript, but i havent yet figured it out how to do it. Any help is appreciated.
I have a search parameter called "search" at request body (req.body.search). I would like find all programs which name contains that search criteria. Ie. "harry" would return "Harry Potter", "Potter Harry", "How merry Harry Really is?" and other movies which contains harry in its name.
exports.programs = function(db, req) {
return function(req, res) {
var collection = db.get('programs');
collection.find({name: /req.body.search/ },{},function(e,docs){
res.json(docs);
});
};
};
回答1:
I'm not familiar with the monk library but if the parameter in that find is a MongoDB Query Document this is the syntax you are looking for:
var query = {
name: {
$regex: req.body.search,
$options: 'i' //i: ignore case, m: multiline, etc
}
};
collection.find(query, {}, function(e,docs){});
Chekcout MongoDB $regex Documentaiton.
来源:https://stackoverflow.com/questions/22021576/like-search-into-mongodb-with-monk-library