Like search into MongoDB with Monk library

廉价感情. 提交于 2021-02-10 20:47:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!