Blog

  • Graph lookup in mongoDB

    Posted by Pradeep Singh | Last Updated: 29-Aug-18

    Introduction:-

    As we know that MongoDB is a Non-Relational Database which is written in C++, Because of Non-Relational database MongoDB doesn't support Join operation directly so that's why MongoDB gives the feature of $lookup and $graphlookup these both are the aggregation operation which is used for the Left outer join so in this blog I will tell you how we can use $graphlookup for recursive search in the same document.

     

    Graph lookup search process is as follows:-

    1.$graphLookup is an aggregation operation which works on the input document.

    2.$graphLookup target the search to the collections designated by the form of parameters.

    3. For-each input document, the search starts with the value designated by "startWith".

    4.$graphLookup matches the start with value against the field designated by connectToField in other documents field in the from collection.

    5. For each matching document object, $graphLookup takes the value of the connectFromField and checks every document in the from a document of collection for a matching connectToField value. For each matched, $graphLookup adds the matching document in the from collection to an array field named by the "as" parameter.

    6. This step continues recursively search until any matching documents are founded by graphlookup, or until this aggregation operation reaches the depth of recursion specified by the "maxDepth" parameter. $graphLookup appends the array of object to the input document. $graphLookup returns result in the form of the array after completing its search on all input documents.


    Syntax:

     

                {
                   $graphLookup: {
                   from: ,
                   startWith: ,
                   connectFromField: ,
                   connectToField: ,
                   as: ,
                   maxDepth: ,
                   depthField: ,
                   restrictSearchWithMatch: 
                }
              }
    

    so First i have a collection students which contain student record like this

                { "_id" : 1, "name" : "Dev" }
                { "_id" : 2, "name" : "Eliot", "reportsToId" : "1" }
                { "_id" : 3, "name" : "Ron", "reportsToId" : "2" }
                { "_id" : 4, "name" : "Andrew", "reportsToId" : "2" }
                { "_id" : 5, "name" : "Asya", "reportsToId" : "3" }
                { "_id" : 6, "name" : "Dan", "reportsToId" : "4" }
            

    so every student report to any other student so if i want to search recursevely to know the hierachy i will do like this:-

                db.students.aggregate( [
                     {
                      $graphLookup: {
                      from: "students",
                      startWith: "$reportsTo",
                      connectFromField: "reportsTo",
                      connectToField: "_id",
                      as: "reportingHierarchy"
                   }
                }
             ] )
            
                {
       "_id" : 1,
       "name" : "Dev",
       "reportingHierarchy" : [ ]
    }
    {
       "_id" : 2,
       "name" : "Eliot",
       "reportsTo" : "Dev",
       "reportingHierarchy" : [
          { "_id" : 1, "name" : "Dev" }
       ]
    }
    {
       "_id" : 3,
       "name" : "Ron",
       "reportsTo" : "Eliot",
       "reportingHierarchy" : [
          { "_id" : 1, "name" : "Dev" },
          { "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" }
       ]
    }
    {
       "_id" : 4,
       "name" : "Andrew",
       "reportsTo" : "Eliot",
       "reportingHierarchy" : [
          { "_id" : 1, "name" : "Dev" },
          { "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" }
       ]
    }
    {
       "_id" : 5,
       "name" : "Asya",
       "reportsTo" : "Ron",
       "reportingHierarchy" : [
          { "_id" : 1, "name" : "Dev" },
          { "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" },
          { "_id" : 3, "name" : "Ron", "reportsTo" : "Eliot" }
       ]
    }
    {
       "_id" : 6,
       "name" : "Dan",
       "reportsTo" : "Andrew",
       "reportingHierarchy" : [
          { "_id" : 1, "name" : "Dev" },
          { "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" },
          { "_id" : 4, "name" : "Andrew", "reportsTo" : "Eliot" }
       ]
    }
            

    For more Information see this link https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/
     

    Thanks

Tags: mongodb