如何在嵌入式数组文档中使用图形查找聚合 [英] How to use graph lookup aggregation in a embedded array document
问题描述
这是我的NetworkInfo集合中的示例文档.
Here is my sample document from NetworkInfo collection.
{
"_id" : ObjectId("5a37595bd2d9ce37f86d612e"),
"edgeList" : [
{
"networkSource" : {
"sourceId" : "pump1"
},
"networkRelationship" : {},
"networkTarget" : {
"targetId" : "chiller1",
"parentId" : "pump1"
}
},
{
"networkSource" : {
"sourceId" : "chiller1"
},
"networkRelationship" : {},
"networkTarget" : {
"targetId" : "secondaryPump1",
"parentId" : "chiller1"
}
},
{
"networkSource" : {
"sourceId" : "secondaryPump1"
},
"networkRelationship" : {},
"networkTarget" : {
"targetId" : "ahu1",
"parentId" : "secondaryPump1"
}
}
]
}
我尝试使用以下代码为上述文档创建图形查找:
I tried to create a graph lookup for the above document using the below code:
pump1-> chiller1-> secondary pump1-> ahu1
pump1->chiller1->secondary pump1->ahu1
db.getCollection("NetworkInfo").aggregate([ {$project:{_id:0}},{ $unwind : "$edgeList" }, { $out : "FlattenedNetwork" } ])
db.FlattenedNetwork.aggregate( [
{
$graphLookup: {
from: "FlattenedNetwork",
startWith: "$edgeList.networkTarget.parentId",
connectFromField: "edgeList.networkTarget.parentId",
connectToField: "edgeList.networkTarget.targetId",
as: "reportingHierarchy"
}}])
这很好.但是,我希望避免使用临时集合"FlattenedNetwork".我尝试添加多个聚合函数,但没有帮助.
This works fine. But, I wish to avoid using the temporary collection "FlattenedNetwork". I tried adding multiple aggregation functions but it didn't help.
推荐答案
我尝试了几次,但没有找到真正的解决方案.我还观看了网络研讨会并且这种情况不包括在内.因此,我决定悬赏这个问题,希望其他人可以分享比我更好的解决方案. 但是,唯一的出路(我认为)是使用这样声明的视图:
I made several tries but I did not find a real solution to this. I also watched the Webinar and this case is not covered. For this reason I decided to put a bounty on this question, with the hope that someone else could share a solution better than mine. However, the only way out (in my opinion) is using a view, that I declared like this:
db.createView("unwounded_docs", "NetworkInfo", [
{
$unwind : "$edgeList"
},
{
$replaceRoot : {
newRoot : "$edgeList"
}
},
{
$project : {
"networkTarget" : 1
}
},
{
$addFields: {
"_id": "$networkTarget.targetId"
}
}
]
);
为了清楚起见,我删除了所有无用的字段. 该视图将具有以下输出:
I removed all useless fields just for clarity. The view will have this output:
{
"networkTarget" : {
"targetId" : "chiller1",
"parentId" : "pump1"
},
"_id" : "chiller1"
},
{
"networkTarget" : {
"targetId" : "secondaryPump1",
"parentId" : "chiller1"
},
"_id" : "secondaryPump1"
},
{
"networkTarget" : {
"targetId" : "ahu1",
"parentId" : "secondaryPump1"
},
"_id" : "ahu1"
}
由于您可以在$graphLookup
阶段的from
字段中引用视图,因此这是管道(至少比以前短):
Since you can refer to a view in the from
field of the $graphLookup
stage, this is the pipeline (at least shorter than before):
db.unwounded_docs.aggregate( [
{
$graphLookup: {
from: "unwounded_docs",
startWith: "$networkTarget.parentId",
connectFromField: "networkTarget.parentId",
connectToField: "networkTarget.targetId",
as: "reportingHierarchy"
}
}])
这篇关于如何在嵌入式数组文档中使用图形查找聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!