【问题标题】:Use $regex inside $expr in mongodb aggregation在 mongodb 聚合中使用 $expr 内的 $regex
【发布时间】:2020-08-12 19:00:28
【问题描述】:

我的文档如下所示

doc = {
    name: 'abc',
    age:20
}

我的查询看起来像

{ $expr: {$and:[{ $gt:[ "$age",  10 ] },
                { $regex:["$name",'ab']}
               ]
          }
} }

但它不工作,我得到一个错误

无法识别的表达式'$regex'

我怎样才能让它工作?

我原来的查询是这样的

db.orders.aggregate([{
$match: {}},
{$lookup: {
    from: "orders",
    let: {
        "customer_details": "$customerDetails"
    },
    pipeline: [
        {
            $match: {
                $expr: {
                        $and: [
                                { $or: [
                                                {
                                                $eq: ["$customerDetails.parentMobile","$$customer_details.parentMobile"]
                                                },
                                                {$eq: ["$customerDetails.studentMobile","$$customer_details.parentMobile"]
                                                },
                                                {$eq: ["$customerDetails.studentMobile","$$customer_details.parentMobile"]
                                                },
                                                {$eq: ["$customerDetails.studentMobile","$$customer_details.studentMobile"]
                                                }
                                            ]
                                        },
                                {$eq: ["$customerDetails.zipCode","$$customer_details.zipCode"]},
                                {$eq: ["$customerDetails.address","$$customer_details.address"]}
                        ]
                    }

            }
        }],
    as: "oldOrder"
}
}])

我想使用regex 匹配address

任何帮助将不胜感激。提前致谢。

【问题讨论】:

  • 你能添加一个数据样本吗?看来你是从同一个集合“订单”中查找的?

标签: regex mongodb mongodb-query aggregation-framework expr


【解决方案1】:

$regex 是一个查询运算符,您不能在 $expr 中使用,因为它只支持聚合管道运算符。

{
  "$expr": { "$gt": ["$age", 10] } ,
  "name": { "$regex": "ab" }
}

如果你有mongodb4.2,可以使用$regexMatch

{ "$expr": {
  "$and": [
    { "$gt": ["$age", 10] },
    {
      "$regexMatch": {
        "input": "$name",
        "regex": "ab", //Your text search here
        "options": "i",
      }
    }
  ]
}}

【讨论】:

  • 如何在 expr 中进行模糊匹配,因为我在 $expr 中使用了 $and
  • 以上查询将满足$and 条件。顺便说一句,你的 mongodb 版本是什么?
  • $regexMatch 的问题是,我在 $lookup 的匹配中使用这个 $expr,在 $lookup 中我使用变量声明并在 $regexMatch 中使用这些变量作为$regexMatch: {input :"$name" , regex:"$$NAME"} 其中@ 987654333@ 是 $lookup 中声明的变量
  • @ganesh 这是有效的mongoplayground.net/p/kwMV8iHIVQK
  • 请出示您的实际文件和查询,然后只有我可以提供帮助。
【解决方案2】:

如果你的mongoDB版本是4.2,那么你可以使用$regexMatch

试试这个

db.collection.find({
  $expr: {
    $and: [
      {
        $gt: [
          "$age",
          10
        ]
      },
      {
        $regexMatch: {
          input: "$name",
          regex: "ab"
        }
      }
    ]
  }
})

检查这个Mongo Playground

【讨论】:

  • 如果我想将"ab" 作为"$$name" 之类的变量而不是硬编码字符串传递怎么办
  • 能否将整个查询添加到问题中?
  • 和您的数据样本,以及预期的输出
猜你喜欢
  • 2013-04-21
  • 2015-07-04
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 2017-09-03
  • 1970-01-01
  • 2021-06-11
相关资源
最近更新 更多