【问题标题】:Lookup with concat followed by match, doesn't return the result of the latter使用 concat 后跟 match 进行查找,不返回后者的结果
【发布时间】:2020-01-08 12:22:16
【问题描述】:

我是 MongoDB 新手,我正在尝试执行多个集合的聚合以验证 HTTP 请求。现在我需要查找一个集合,将我查找过的集合的值与给定的字符串连接起来,并在匹配等于时使用连接的结果。下面的代码是我目前拥有的:

{
  from: "Patient",
  let: {
    subject: "$subject.reference"
  },
  pipeline: [
    {
      $project: {
        concatTest: {
          $concat: [
            "Patient/",
            "$id"
          ]
        }
      }
    },
    {
      $match: {
        $expr: {
          $eq: [
            "$concatTest",
            "$$subject"
          ]
        }
      }
    }
  ],
  as: "result"
}

问题:

result数组不输出经过match过滤后的集合,而是输出concat的结果如图:

result:Array
0:Object
_id:5d6d13175def3532dd905767
concatTest:"Patient/5d6d13175def3532dd905767"

我猜这是放置正确输出的一个非常简单的问题,但是我找不到解决方案。也许我不应该在管道内做 concat ?还是我完全误解了管道的工作原理?

提前致谢。

【问题讨论】:

    标签: mongodb lookup concat


    【解决方案1】:

    是的,这只是一个简单的问题,即 concat 是管道的一部分,而不是在匹配中。解决方法是把匹配写成如下:

          $match: {
            $expr: {
              $eq: [
                "$$subject",
                  {$concat:["Patient/","$id"]}
              ]
            }
          }
    

    这样,concat 被解析为匹配而不是管道。

    【讨论】:

      猜你喜欢
      • 2018-03-06
      • 2014-11-21
      • 2017-06-24
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多