[英]Mongo $lookup $pipeline with several $expr parameters not returning correct data in $lookup results
[英]$in/$eq in mongo pipeline $lookup is not working
我在quotes_id
上有一个包含字符串值数组的集合details
,如下所示
{
"quantity": 2000,
"quotes_id": ["SQ-CPQ10037"],
"status": "processed",
"logs": [""],
"product_id": "5ff5cf3dc1ceb9144901da81"
}
还有我的quotes
集
{
"_id":"SQ-CPQ10037",
"product_id":"5ff5cf3dc1ceb9144901da81",
...more fields here
}
现在,我正在尝试使用查找管道从另一个名为quotes
的集合中访问此集合,如下所示
$lookup: {
from: "details",
"let": { "product_id": "$quotes.product_id", "quotes_id": "$quotes._id"},
pipeline: [
{
$match: {
$expr: {
$and: [
{ $in: [ "$quotes_id", ["$$quotes_id"] ] },
{ $eq: [ "$product_id", "$$product_id" ] },
]
}
}
}],
as: "product.productquotes",
},
这是返回空集合。 但是,如果我评论该行
{ $in: [ "$quotes_id", ["$$quotes_id"] ] },
然后它把记录还给我。 我还尝试使用简单的$eq
作为quotes_id
,但结果相同。 但是如果我直接运行查询
{$and: [{"product_id": ObjectId('5ff5cf3dc1ceb9144901da81')}, {"quotes_id": "SQ-CPQ10037"}]}
在 mongo 指南针上,我收到了没有任何问题的记录。
你做的几乎是正确的。 但是错误地写了$in
参数。 in 的第二个参数应该是一个数组。
{
$in: [
"$$quotes_id",
"$quotes_id"
]
}
所以查找喜欢
db.quotes.aggregate([
{
$lookup: {
from: "details",
"let": { "product_id": "$product_id", "quotes_id": "$_id" },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $in: [ "$$quotes_id", "$quotes_id" ] },
{ $eq: [ "$product_id", "$$product_id" ] }
]
}
}
}
],
as: "product.productquotes"
}
}
])
工作蒙戈游乐场
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.