Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex node.js中Mongo db的正则表达式不工作_Regex_Node.js_Mongodb - Fatal编程技术网

Regex node.js中Mongo db的正则表达式不工作

Regex node.js中Mongo db的正则表达式不工作,regex,node.js,mongodb,Regex,Node.js,Mongodb,我正在使用正则表达式从我的节点js应用程序中从mongodb获取数据,该应用程序不工作。下面是代码片段。查询无法与记录集匹配 var analysis_date = '/^'+moment(fromTime).format('YYYY-MM').toString()+'/'; user_analysis.find({ parent_id: usrId, analysis_date: { $regex : analysis_date } },function(err, resul

我正在使用正则表达式从我的节点js应用程序中从mongodb获取数据,该应用程序不工作。下面是代码片段。查询无法与记录集匹配

var analysis_date = '/^'+moment(fromTime).format('YYYY-MM').toString()+'/';
user_analysis.find({
parent_id: usrId,
analysis_date: { 
    $regex : analysis_date 
   }
},function(err, result) {
//some Code goes here});

尝试传递
RegExp
对象,而不是将正则表达式指定为字符串:

user_analysis.find({
    "parent_id": usrId,
    "analysis_date": new RegExp(analysis_date, 'i')
}, function(err, result) {
    // ...
});
如果需要,还可以传递字符串,但需要删除
/
分隔符:

user_analysis.find({
    "parent_id": usrId,
    "analysis_date": {
        $regex: '^' + moment(fromTime).format('YYYY-MM').toString(),
        $options: "i"
    }
}, function(err, result) {
    // ...
});

你设法解决了这个问题吗?是的,它用你提到的第二种方法解决了。