假设我有一个只有一个集合的 Mongodbdata
。在这个集合中,我有以下文件:
{
"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
}
}
有了这些记录,我想在文档之间进行 JOIN"type": person
and "type": prescription
on value.id = value.patient
.
我已经尝试过以下阶段的聚合:
{
"$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
.
知道如何进行此聚合吗?