How to use graph aggregation in an embedded array document

Here is my sample document from the 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 search for the above document using the code below:

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"
  }}])

It works great. But I want to avoid using the temporary collection "FlattenedNetwork". I tried to add some aggregation functions, but that did not help.

+6
source share
2 answers

I do not have enough reputation to comment, so I am posting this as an answer, sorry in advance!

@ S.patil have you ever determined a solution to this issue? I am trying to create a similar query.

0
source

, . , . , , - . ( ) - , :

db.createView("unwounded_docs", "NetworkInfo", [ 
        {
            $unwind : "$edgeList"
        }, 
        {
            $replaceRoot : {
                newRoot : "$edgeList"
            }
        }, 
        {
            $project : {
                "networkTarget" : 1
            }
        },
        {
            $addFields: {
                "_id": "$networkTarget.targetId"
            }
        }
    ]
);

. :

{
    "networkTarget" : {
        "targetId" : "chiller1",
        "parentId" : "pump1"
    },
    "_id" : "chiller1"
},
{
    "networkTarget" : {
        "targetId" : "secondaryPump1",
        "parentId" : "chiller1"
    },
    "_id" : "secondaryPump1"
},
{
    "networkTarget" : {
        "targetId" : "ahu1",
        "parentId" : "secondaryPump1"
    },
    "_id" : "ahu1"
}

from $graphLookup , ( , , ):

db.unwounded_docs.aggregate( [
{
  $graphLookup: {
     from: "unwounded_docs",
     startWith: "$networkTarget.parentId",
     connectFromField: "networkTarget.parentId",
     connectToField: "networkTarget.targetId",
     as: "reportingHierarchy"
  }
}])
0

Source: https://habr.com/ru/post/1691022/


All Articles