$lookup 中的 $match 字段从一个集合到另一个集合中的字段

$match field from one collection to field in another inside $lookup

我正在尝试找到与 TestJob 对应的 TestRun,文档在“名称”字段上匹配。我对 $match 有疑问,也许我不明白 $let 应该如何工作?

testJob 文档

{
  "_id": {
    "$oid": "60409e6ea7a5b30261ebf380"
  },
  "database": {...
  },
  "product": [
    "DBCOPY"
  ],
  "func": [
    "torch it"
  ],
  "case": [
    "21521338-01"
  ],
  "problem": [],
  "tk": [],
  "bucket": [
    "batch"
  ],
  "version": [
    "14",
    "15"
  ],
  "release": [
    "20.0"
  ],
  "name": "CICSTKIC",
  "location": "THERE.IT.IS(CICSTKIC)",
  "knownErrors": ""
}

测试运行文档

{
  "_id": {
    "$oid": "5f6b27e4a3831fae28eb6867"
  },
  "name": "CICSTKIC",
  "library": "HUR.DUR.LOAD",
  "version": "15",
  "release": "20.0",
  "bucket": "batch",
  "runDate": {
    "$date": "2020-09-23T10:47:58.794Z"
  },
  "ranAs": "DODO",
  "user": "DIDI",
  "status": "submitted",
  "jobNumber": "JOB40854",
  "rerun": false,
  "__v": 0
}

我的 $lookup 怎么了? trs 数组为空,如果我将 $match 硬编码为“CICSTKIC”,它会工作

{ $lookup:
    {
      from: 'testRuns',
      as: 'trs',
      let: { nam: '$name' }, // does this save reference to testJob.name for further use?
      pipeline: [
        { $match: {
          name: '$$nam' // testRun.name to match testJob.name
        } },
        { $sort: { runDate: 1 } }, 
        { $limit: 10 }
      ]
    }
}
{ $lookup: {
        from: 'testRuns',
        let: {
          testName: '$name'
        },
        pipeline: [
          { $match: {
              $expr: {
                $eq: [
                  "$$testName",
                  "$name"
                ]
              },
            }},
          { $project: { _id : 0, runDate: 1, status: 1, jobNumber: 1 } },
          { $sort: { runDate: -1 } },
          { $limit: 1}
        ],
        as: "lastRun"
      }
    }