下面是一些中间管道的聚合
[
{
$unwind: "$destinations"
},
{
$lookup: {
from: "customers",
localField: "destinations.sold_to_id",
foreignField: "_id",
as: "sold_to_ref"
},
},
]
根据上面的查询,destinations
是一个数组,如下所示
[{
type: 1,
sold_to_id: 'xxxxx'
},
{
type: 2,
sold_to_id: 'yyyy',
}
]
现在我想根据type(ie)执行查找,如果type = 1,则从customers
查找,否则从users
查找。注意:customers
和users
是两个不同的集合
请帮我在这。
先谢谢你了
编辑:
我尝试的解决方案
[{
$unwind: "$destinations"
},
{
$lookup: {
from: "customers",
"let": {
type: "$destinations.type",
destination_id: "$destinations.sold_to_id"
},
pipeline: [
{
"$match": {
"$expr": { $and:
[
{ $eq: [ "$$type", 1 ] }, // Here I need to compare the type value with 1 which is not happening
{ $eq: [ "$_id", "$$destination_id" ] }
]
}
}
}
],
as: "sold_to_ref"
},
}]
编辑:如果输入集合类似于
db={
"collection": [
{
"contact": [
'adf', 'dsf', 'sdd'],
"destinations": [
{
type: 1,
sold_to_id: "xxxxx"
},
{
type: 1,
sold_to_id: "yyyy"
},
{
type: 1,
sold_to_id: "zzz"
},
{
type: 1,
sold_to_id: "zzz"
}
]
}
],
"customers": [
{
_id: "xxxxx",
name: "Customer1"
},
{
_id: "yyyy",
name: "Customer2"
}
],
"users": [
{
_id: "xxxxx",
name: "User1"
},
{
_id: "yyyy",
name: "User2"
}
]
}
则结果应如下
[
{
"_id": ObjectId("5a934e000102030405000000"),
"contact": [
'adf', 'dsf', 'sdd'],
"destinations": [
{
type: 1,
sold_to_id: "xxxxx"
},
{
type: 1,
sold_to_id: "yyyy"
},
{
type: 1,
sold_to_id: "zzz"
},
{
type: 1,
sold_to_id: "zzz"
}
]
"sold_to_ref": [
{
"_id": "xxxxx",
"name": "Customer1"
}
]
},
{
"_id": ObjectId("5a934e000102030405000000"),
"contact": [
'adf', 'dsf', 'sdd'],
"destinations": [
{
type: 1,
sold_to_id: "xxxxx"
},
{
type: 1,
sold_to_id: "yyyy"
},
{
type: 1,
sold_to_id: "zzz"
},
{
type: 1,
sold_to_id: "zzz"
}
]
"sold_to_ref": [
{
"_id": "yyyy",
"name": "Customer2"
}
]
},
{
"_id": ObjectId("5a934e000102030405000000"),
"contact": [
'adf', 'dsf', 'sdd'],
"destinations": [
{
type: 1,
sold_to_id: "xxxxx"
},
{
type: 1,
sold_to_id: "yyyy"
},
{
type: 1,
sold_to_id: "zzz"
},
{
type: 1,
sold_to_id: "zzz"
}
]
"sold_to_ref": []
},
{
"_id": ObjectId("5a934e000102030405000000"),
"contact": [
'adf', 'dsf', 'sdd'],
"destinations": [
{
type: 1,
sold_to_id: "xxxxx"
},
{
type: 1,
sold_to_id: "yyyy"
},
{
type: 1,
sold_to_id: "zzz"
},
{
type: 1,
sold_to_id: "zzz"
}
]
"sold_to_ref": []
}
]
1条答案
按热度按时间lndjwyie1#
不可能(即使在SQL中也不可能)。
**解决方法:**使用
$facet
运行2个LEFT JOINS
,合并它们并展平结果。MongoPlayground