我创建了以下视图。如果不是我仍然需要订单头文档,我想让所有客户使用addresstype结算订单。我认为匹配阶段正在消除少量文档,因为几乎没有文档的地址类型计费。
我的意思是它的表现就像INNER JOIN,而我想要LEFT JOIN。尽力而为,但我得到的只是内心加入。
db.createView(
"VWOrderHeader",
"OrderHeader",
[
{
$lookup:{
from:"AddressDoc",
as: "Address",
localField: "CustomerNo",
foreignField: "CustomerNo"
}
},
{
$match:{ "Address.AddressType":/Billing/i }
},
{
$project:{
_id:0,
CustomerNo :"$CustomerNo",
ContactName:{ $ifNull:[{$arrayElemAt: [ "$Address.ContactName",0 ]},""]}
}
}
]
)}
答案 0 :(得分:0)
重新编写查询
db.createView(
"VWOrderHeader",
"OrderHeader",
[
{
$lookup:{
from:"AddressDoc",
as: "Address",
localField: "CustomerNo",
foreignField: "CustomerNo"
}
},
{ $unwind: { path: "$Address", preserveNullAndEmptyArrays: true } },
{
$match:{ $or:[ {"Address.AddressType":/billing/i},{"Address.AddressType":""},{"Address.AddressType":null}] }
},
{
$project:{
_id:0,
CustomerNo :"$CustomerNo",
ContactName:{ $ifNull:[ "$Address.ContactName",""]}
}
}
]
)}