无法在聚合查询中的mongoose中使用$regex?
在mongoose聚合查询中使用$regex时出错。我得到一个错误号为15999的无效运算符。请帮帮我无法在聚合查询中的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: {
{ $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"))});