简体   繁体   English

MongoDB 让变量在管道聚合中不起作用

[英]MongoDB let variable not working in pipeline aggregation

Hi I am trying to use a variable that I defined in let to be used in the match lookup, but it returns no results when regex is used:嗨,我正在尝试使用我在 let 中定义的变量用于匹配查找,但是在使用正则表达式时它不返回任何结果:

It works like this:它是这样工作的:

db.MSP_Prosper.aggregate([
    { $match: {Cleavage_score: {$gte:0.7}}},
    { $lookup: {
      from:"Uniprot_New_Entries",
      let: { order_item: new RegExp('.*' + "P62258" + '.*')},
      pipeline: [{
        $match: { Uniprot_AC : new RegExp('.*' + "P62258" + '.*')                                      
      }},
      { $project: { _id: 0,
        test: "$$order_item",
        date: { name: "$Uniprot_ID",
        date: "$Min_Max_Of_the_Ft_chain"} 
      }
    }],
       as:"cleavage_sites"
    }   
 }

]) ])

but not when I try to use same variable defined in let function:但当我尝试使用 let 函数中定义的相同变量时则不然:

db.MSP_Prosper.aggregate([
    {$match: {Cleavage_score: {$gte:0.7}}},
    {
    $lookup: { from:"Uniprot_New_Entries",
           let: { order_item: new RegExp('.*' + "P62258" + '.*')},
           pipeline: [
             { $match: 
               { Uniprot_AC : "$$order_item" }
             },
             { $project: { _id: 0,
                  test: "$$order_item",
                  date: { name: "$Uniprot_ID",
                          date: "$Min_Max_Of_the_Ft_chain"} 
             }},    
           ],
           as:"cleavage_sites"
    }   
  }
])

Ultimatelly I want to replcae the "P62258" with a local variable $Protein_ID最终,我想用局部变量 $Protein_ID 来替换“P62258”

Hope you can help, Have tried out everything with no success.希望你能帮忙,已经尝试了一切都没有成功。

The new RegExp is evaluated in the client and included in the query as an absolute value, so you cannot include a document-dependent variable that way. new RegExp在客户端进行评估并作为绝对值包含在查询中,因此您不能以这种方式包含文档相关变量。

To use a document field as a regular expression inside of a lookup pipeline, you will need to use a $expr clause with the $regexMatch aggregation operator.要将文档字段用作查找管道内的正则表达式,您需要将$expr子句与$regexMatch聚合运算符一起使用。 Note that the .* before and after the search string are implied, and therefore unnecessary.请注意,搜索字符串前后的.*是隐含的,因此是不必要的。

Something like:就像是:

db.MSP_Prosper.aggregate([
  { $match: { Cleavage_score: { $gte: 0.7 } } },
  { $lookup: {
      from: "Uniprot_New_Entries",
      let: { "order_item": "$Protein_ID" },
      pipeline: [
        {$match: {
            $expr: {
              $regexMatch: {
                input: "$Uniprot_AC",
                regex: "$$order_item"
        }}}},
        {$project: {
            _id: 0,
            test: "$$order_item",
            date: {
              name: "$Uniprot_ID",
              date: "$Min_Max_Of_the_Ft_chain"
        }}},
      ],
      as: "cleavage_sites"
  }}
])

Playground操场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM