Mongo 3.6 多条件聚合查找

Mongo 3.6 aggregation lookup with multiple conditions

假设我有一个 Mongo 数据库,只有一个集合 data。在这个集合中,我有以下文件:

{
    "type": "person",
    "value": {
        "id": 1,
        "name": "Person 1",
        "age": 10
    }
},
{
    "type": "person",
    "value": {
        "id": 2,
        "name": "Person 2",
        "age": 20
    }
},
{
    "type": "prescription",
    "value": {
        "drug": "Bromhexine",
        "patient": 2
    }
},
{
    "type": "prescription",
    "value": {
        "drug": "Aspirin",
        "patient": 1
    }
}

有了这些记录,我想在 value.id = value.patient"type": person"type": prescription 的文档之间进行 JOIN。

我已经尝试过以下阶段的聚合:

{
    "$match": {
        "type": "person"
    }
},
{
    "$lookup": {
        "from": "data",
        "let": { "patient": "$value.id"},
        "pipeline": [
            {
                "$match": {
                    "$expr": {
                        "type": "prescription",
                        "value.patient": "$$patient"
                    }
                }
            }
        ],
        "as": "prescription"
    }
}

但它会产生错误 FieldPath field names may not contain '.'。我认为这是由于 "let": { "patient": "$value.id"}, 行。相反,如果我尝试使用双美元符号 ($$)(如 here 所示),结果是错误 Use of undefined variable: value.

知道如何进行聚合吗?

$lookup 阶段

中完成以下管道对象
"pipeline": [
                { "$match":
                    { "$expr":
                        { "$and":
                            [
                                { "$eq": [ "$type", "prescription" ] },
                                { "$eq": [ "$value.patient",  "$$patient" ] }
                            ]
                        }
                    }
                }
            ]