【发布时间】:2018-01-30 18:44:55
【问题描述】:
假设我有一个只有一个集合 data 的 Mongo DB。在这个集合中,我有以下文档:
{
"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。
知道如何进行这种聚合吗?
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework lookup aggregation