繁体   English   中英

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

[英]MongoDB let variable not working in pipeline aggregation

嗨,我正在尝试使用我在 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 : new RegExp('.*' + "P62258" + '.*')                                      
      }},
      { $project: { _id: 0,
        test: "$$order_item",
        date: { name: "$Uniprot_ID",
        date: "$Min_Max_Of_the_Ft_chain"} 
      }
    }],
       as:"cleavage_sites"
    }   
 }

])

但当我尝试使用 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"
    }   
  }
])

最终,我想用局部变量 $Protein_ID 来替换“P62258”

希望你能帮忙,已经尝试了一切都没有成功。

new RegExp在客户端进行评估并作为绝对值包含在查询中,因此您不能以这种方式包含文档相关变量。

要将文档字段用作查找管道内的正则表达式,您需要将$expr子句与$regexMatch聚合运算符一起使用。 请注意,搜索字符串前后的.*是隐含的,因此是不必要的。

就像是:

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"
  }}
])

操场

暂无
暂无

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

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