Mongodb Mongo 3.6具有多个条件的聚合查找
假设我有一个Mongo DB,它只有一个集合Mongodb Mongo 3.6具有多个条件的聚合查找,mongodb,mongodb-query,aggregation-framework,lookup,aggregation,Mongodb,Mongodb Query,Aggregation Framework,Lookup,Aggregation,假设我有一个Mongo DB,它只有一个集合数据。在此集合中,我拥有以下文档: { "type": "person", "value": { "id": 1, "name": "Person 1", "age": 10 } }, { "type": "person", "value": { "id": 2, "name": "Person 2", "age":
数据。在此集合中,我拥有以下文档:
{
"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”:处方的文档之间建立一个连接
我已经尝试了以下阶段的聚合:
{
"$match": {
"type": "person"
}
},
{
"$lookup": {
"from": "data",
"let": { "patient": "$value.id"},
"pipeline": [
{
"$match": {
"$expr": {
"type": "prescription",
"value.patient": "$$patient"
}
}
}
],
"as": "prescription"
}
}
但它会产生一个错误字段路径字段名不能包含“.”
。我认为这是由于“let”:{“patient”:“$value.id”},
行。如果我尝试使用双美元符号($$)(如图所示),结果是错误使用未定义变量:value
知道如何进行此聚合吗?使用$lookup
阶段中的以下管道对象完成
"pipeline": [
{ "$match":
{ "$expr":
{ "$and":
[
{ "$eq": [ "$type", "prescription" ] },
{ "$eq": [ "$value.patient", "$$patient" ] }
]
}
}
}
]