【问题标题】:Regex inside mongodb lookup pipeline stagemongodb查找管道阶段内的正则表达式
【发布时间】:2020-07-09 23:00:00
【问题描述】:

如何在lookuppipeline 阶段执行regex 搜索?

{
    $lookup: {
        from: 'collection',
        let: {variable: '$localVariable', name: '$name'},
        pipeline:[{
            $match:{
                $expr:{
                    $and: [
                        {'$name': {$regex: '.*$$name.*', $options: 'i'}},
                        {$in: ['$_id', '$$variable']}
                    ]
                }
            }
        }],
        as: 'newDocument'
    }
},

我已经尝试了几件事,我很确定应该将正则表达式阶段包装在另一个运算符中,只是不确定是哪一个?

【问题讨论】:

标签: mongodb


【解决方案1】:

对于 Mongo 4.2+ 版本,您可以使用 $regexMatch

db.collection.aggregate([
  {
    $lookup: {
      from: "other",
      let: {
        name: "$name"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $regexMatch: {
                input: "$name",
                regex: "$$name",
                options: "i"
              }
            }
          }
        }
      ],
      as: "newDocument"
    }
  }
])

Mongo Playground

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 2021-09-03
    相关资源
    最近更新 更多