Javascript mongoose“or”和“like”操作符node.js

Javascript mongoose“or”和“like”操作符node.js,javascript,regex,node.js,mongodb,Javascript,Regex,Node.js,Mongodb,我想实现对我的页面的搜索。所以从前端我从用户的输入中得到了搜索值,我需要在我的数据库中搜索,限制为3行。 所以我需要这样的sql查询: 从产品p中选择*,其中p.title类似于“%user\u value%”或p.sku类似于“%user\u value%” 我试着这样做: router.get('/search', function(req, res, next) { var value = req.query.val; var query = Product.find({"

我想实现对我的页面的搜索。所以从前端我从用户的输入中得到了搜索值,我需要在我的数据库中搜索,限制为3行。 所以我需要这样的sql查询: 从产品p中选择*,其中p.title类似于“%user\u value%”或p.sku类似于“%user\u value%”

我试着这样做:

router.get('/search', function(req, res, next) {
    var value = req.query.val;
    var query = Product.find({"title":  new RegExp('/' + value+'/')}).limit(3);

    query.exec(function(err, products) {
        if (!err) {
            // Method to construct the json result set
            var result =  JSON.stringify(products);
            log.info(products);

            res.send(result, {
                'Content-Type': 'application/json'
            }, 200);
        } else {
            res.send(JSON.stringify(err), {
                'Content-Type': 'application/json'
            }, 404);
        }
    });
Product.find({"title": new RegExp(".*" + value + ".*")}).limit(3);
但我总是得到空洞的结果。在我看来,RegExp路径无效

更新1: “like”运算符固定为:

router.get('/search', function(req, res, next) {
    var value = req.query.val;
    var query = Product.find({"title":  new RegExp('/' + value+'/')}).limit(3);

    query.exec(function(err, products) {
        if (!err) {
            // Method to construct the json result set
            var result =  JSON.stringify(products);
            log.info(products);

            res.send(result, {
                'Content-Type': 'application/json'
            }, 200);
        } else {
            res.send(JSON.stringify(err), {
                'Content-Type': 'application/json'
            }, 404);
        }
    });
Product.find({"title": new RegExp(".*" + value + ".*")}).limit(3);

但是,在RegExp对象中,像“%user\u value%”这样的p.sku问题仍然存在

,您应该执行以下操作:

new RegExp(value)
与此相反:

new RegExp('/' + value + '/')
请参阅此链接:

您需要将修饰符作为第二个参数传递给RegExp构造函数

var query = Product.find({"title": new RegExp(".*" + value.replace(/(\W)/g, "\\$1") + ".*", "i")}).limit(3);
例如:


您是否试图在RegExp中包含变量value?然后var query=Product.find{title:newregexpvalue}.limit3;嗯,只需要值,但我需要像%value%?尝试var query=Product.find{title:new RegExp.*+value+.*}.limit3;thx,这是可行的,但我如何添加case ignore,我应该把'I'attr'放在哪里?我有一些问题,然后值变量有像'SNR-SFP+W73-60'这样的文本,它找不到任何东西,但必须工作!非常感谢