Regex node.js中Mongo db的正则表达式不工作
我正在使用正则表达式从我的节点js应用程序中从mongodb获取数据,该应用程序不工作。下面是代码片段。查询无法与记录集匹配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
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) {
// ...
});
你设法解决了这个问题吗?是的,它用你提到的第二种方法解决了。