假设我有一个只有一个集合的Mongo DB
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
}
}
有了这些记录,我想用
"type": person
和
"type": prescription
在…上
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
.
你知道我怎么做这个聚合吗?