Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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
无法在聚合查询中的mongoose中使用$regex?_Regex_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

无法在聚合查询中的mongoose中使用$regex?

无法在聚合查询中的mongoose中使用$regex?,regex,node.js,mongodb,express,mongoose,Regex,Node.js,Mongodb,Express,Mongoose,在mongoose聚合查询中使用$regex时出错。我得到一个错误号为15999的无效运算符。请帮帮我 { $match: { "_id": ObjectId(req.headers.token) } }, { $project: { inventory: { $filter: {

在mongoose聚合查询中使用$regex时出错。我得到一个错误号为15999的无效运算符。请帮帮我

{ $match: { "_id": ObjectId(req.headers.token) } }, {
                        $project: {
                            inventory: {
                                $filter: {
                                    input: '$inventory',
                                    as: 'inventory',
                                    cond: {$or:[{"$$inventory.item":new RegExp('^'+req.query.text+'$', "i")},{"$$inventory.sku":new RegExp('^'+req.query.text+'$', "i")}]}
                                }
                            },
                            _id: 0
             }
  }

您可以在$match中使用regex,您将得到与本例相同的结果

Pincodes.aggregate([
{ "$match": { "district": new RegExp('^' + req.query.district, "i") } 
},
{ "$group": { "_id": "$district" } }])
.exec((err, data) => {
if (err) {
res.status(500).json({
data: err
})
}
else {
res.status(200).json({
data: data
})
}
})

只做
agg.match({:eval(newregexp('^'+req.query.district,“i”))});

可以将Rq.Que..Text考虑为使用JS代码,尝试将其作为<代码> Req.Que.Text进行替换。(//\\\**+..[()] [\] {}}/g,\\\$&')< />。如果文本包含
[
或其他特殊的正则表达式元字符,则应该可以解决此问题。不确定是否可以在$project中使用正则表达式
agg.match({'<fieldName'>:eval(new RegExp('^' + req.query.district, "i"))});