我的
claim_rules
收藏看起来像这样
[
{
"_id": {"$oid": "66c3118a234ecb42936c6dba"},
"ramo_sx": 1,
"tipo_sx": 700201
},
{
"_id": {"$oid": "66c3118a234ecb42936c6dbb"},
"ramo_sx": 1,
"tipo_sx": 700202
},
{
"_id": {"$oid": "66c3118a234ecb42936c6dbc"},
"ramo_sx": 1,
"tipo_sx": 700208
},
{
"_id": {"$oid": "66c3118a234ecb42936c6dbd"},
"ramo_sx": 1,
"tipo_sx": 700210
},
{
"_id": {"$oid": "66c3118a234ecb42936c6dbe"},
"ramo_sx": 1,
"tipo_sx": 700219
},
{
"_id": {"$oid": "66c3118a234ecb42936c6dbf"},
"ramo_sx": 1,
"tipo_sx": 900102
},
...
我试图使用具有这样的管道的查找阶段仅查找特定的
ramo_sx
和 tipo_sx
字段
{
$lookup: {
from: "claim_rules",
let: { ramo_sx: "$vda.ramo_sx", tipo_sx: "$vda.tipo_sx" },
pipeline: [
{
$project: {
_id: 0,
}
},
{
$match: {
ramo_sx: "$$ramo_sx",
tipo_sx: "$$tipo_sx"
}
}
],
as: "claim_rules"
}
}
vda
变量取自该集合:
[
{
"_id": "1-8101-2023-0445722",
"vda": {
"ramo_sx": 5,
"tipo_sx": 31
}
},
{
"_id": "1-8101-2023-0447269",
"vda": {
"ramo_sx": 39,
"tipo_sx": 22
}
},
...
问题是生成的
claim_rules
是空的
根据docs,您必须使用
$expr
运算符来访问变量。您的 $lookup
管道应如下所示:
{
$lookup: {
from: "claim_rules",
let: {
ramo_sx: "$vda.ramo_sx",
tipo_sx: "$vda.tipo_sx"
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: [
"$ramo_sx",
"$$ramo_sx"
]
},
{
$eq: [
"$tipo_sx",
"$$tipo_sx"
]
}
]
}
}
},
{
$project: {
_id: 0
}
}
],
as: "claim_rules"
}
}
另外,建议将
$match
阶段放置为第一个阶段,以利用索引并获得更好的查询性能。