title: MongoDB $graphLookup get children all levels deep - nested result
body: <p>As presented in <a href="https://www.slideshare.net/mongodb/webinar-working-with-graph-data-in-mongodb" rel="nofollow noreferrer">https://www.slideshare.net/mongodb/webinar-working-with-graph-data-in-mongodb</a>, <strong>slide 50</strong> it is possible to use <code>$graphLookup</code> on a View in order to get a 2 levels deep tree-structure in <strong>nested format</strong>.</p>
  
  <p>I have a MongoDB collection with tree nodes as documents with the following format:</p>
  
  <pre><code>{ "_id" : { "$oid" : "5b1a952361c6fa3418a15660" }, 
  "nodeId" : 23978995, 
  "name" : "settings", 
  "type" : "Node",
  "parentId" : [ 23978893, 23979072, 23979081 ] }
  </code></pre>
  
  <p>I have created a View like:</p>
  
  <pre><code>db.createView("treeView", "node", [
  {
   $graphLookup: {
      from: "node",
      startWith: "$nodeId",
      connectFromField: "nodeId",
      connectToField: "parentId",
      maxDepth: 0,
      as: "children"
   }
  }
  ]);
  </code></pre>
  
  <p>And I execute graph lookups like:</p>
  
  <pre><code>db.node.aggregate([ 
  { $match: {"nodeId": 23978786 } },
  {
   $graphLookup: {
      from: "treeView",
      startWith: "$nodeId",
      connectFromField: "nodeId",
      connectToField: "parentId",
      maxDepth: 0,
      as: "children"
   }
  }
  ]);
  </code></pre>
  
  <p>My question is how can I get the whole hierarchy, all levels deep? </p>