Mongo Shell版本4.0.2
我有3个集合,集合之间有数据关联
B:
{
"_id" : ObjectId("5b8f83fb520b3ea906ac11d1"),
"X" : "002100367752",
"Z" : "002KV180054605",
}
A :
{
"_id" : ObjectId("5b8f83fa520b3e09e7ac0de1"),
"Y" : "002100367752"
}
C :
{
"_id" : ObjectId("5b8f83fb520b3e0127ac15b9"),
"N" : "002KV180054605",
"Q" : 17,
"W" : 1,
"E" : 35
}
连接看起来像这样:
Collection A <> Collection B <> Collection C
仅在集合B中,我有来自所有3个集合的对象的ID
现在,我想计算集合A中通过集合B连接到具有特定参数的集合C中的对象的不同对象数。
这意味着我的计数应返回集合A中不同对象的数量,这些对象可以与集合C中的对象具有多个连接
如果我查询如下,我得不到任何结果:
db.B.aggregate([
{
$lookup:
{
from: "A",
localField: "X",
foreignField: "Y",
as: "Acc"
}
},
{
$match: { "Acc": { $ne: [] } }
},
{
$lookup:
{
from: "C",
localField: "Z",
foreignField: "N",
as: "Con"
}
},
{
$match: { "Con": { Q: 1, W: 1, E: 9 } }
}
])
此外,我不知道如何计算此查询
我认为这是我的解决方案:
db.idlsfcontractroles.aggregate([
{"$lookup":{
"from":"idlsfcontracts",
"localField":"Contract__r_ExternalId__c",
"foreignField":"ExternalId__c",
"as":"Con"
}},
{"$unwind":"$Con"},
{"$match":{"Con.Status__c":1,"Con.Type__c":1,"Con.ConductingCompany__c":31}},
{"$group":{"_id": null, uniqueValues: {$addToSet: "$Account__r_MDMExternalId__c"}}},
{"$project": {"_id": 1, numberOfParties: { $size: "$uniqueValues" }}}
])