$match вътре в $lookup pipeline няма връзка с jobCollection документи. Филтрира само документите за tasks колекция. Така че трябва да използвате още един $match етап след $lookup за филтриране на ROOT(jobCollection ) документи.
jobCollection.aggregate([
{ "$match": { "$text": { "$search": "1234" }}},
{ "$lookup": {
"from": "task",
"let": { "job_id": "$_id" },
"pipeline": [
{ "$match": {
"$expr": {
"$and": [
{ "$eq": ["$job", "$$job_id"] },
{ "$eq": ["$status", "FAILED"] }
]
}
}}
],
"as": "tasks"
}},
{ "$match": { "tasks": { "$ne": [] }}},
])