title: Aggregate recursive tree structure (parent reference)
body: <p>We are trying to build a simple CMS that can manage pages and its subPages. And every subPage can also have subPages etc.</p>
  
  <p>The Problem we are currently facing is that we receive a flat tree after the second level and we would have to reorganise the tree with JS.</p>
  
  <p>Is it possible to receive a real tree structure (see "desired output") without the use of JS.</p>
  
  <p>The following tree structure is currently used:</p>
  
  <pre><code>[{
      _id: 1,
      name: "top-level",
      parent: null
  },{
      _id: 2,
      name: "second-level-a",
      parent: 1 // top-level
  },{
      _id: 3,
      name: "second-level-b",
      parent: 1 // top-level
  },{
      _id: 4,
      name: "third-level",
      parent: 2 // second-level-a
  }]
  </code></pre>
  
  <p>We are currently trying to receive the complete tree via this query:</p>
  
  <pre><code>Page.aggregate([
      {
          "$graphLookup": {
              "from": "Page",
              "startWith": "$_id",
              "connectFromField": "_id",
              "connectToField": "parent",
              "as": "subPages",
              "maxDepth": 1,
              "depthField": "depth"
          }
      }, {
          "$match": {
              "options.parent": null
          }
      }
  ]).exec((err, pages) =&gt; {
      if (err) {
          res.send(err);
          return;
      }
      res.json(pages);
  });
  </code></pre>
  
  <p>What we are receiving is something like this:</p>
  
  <pre><code>[
      {
          _id: 1,
          name: "first-level",
          parent: null,
          subPages: [
              {
                  _id: 2,
                  name: "second-level-a",
                  depth: 0,
                  [...]
              },{
                  _id: 3,
                  name: "second-level-b",
                  depth: 0,
                  [...]
              },{
                  _id: 4,
                  name: "third-level",
                  depth: 1,
                  [...]
              }
  
          ]
      }
  ]
  </code></pre>
  
  <p>With this output it's not directly visible where third-level is placed inside the tree. The desired output should look like this:</p>
  
  <pre><code>[
      {
          _id: 1,
          name: "first-level",
          parent: null,
          subPages: [
              {
                  _id: 2,
                  name: "second-level-a",
                  depth: 0,
                  [...]
                  subPages: [
                      {
                          _id: 4,
                          name: "third-level",
                          depth: 1,
                          [...]
                      }
                  ]
              },{
                  _id: 3,
                  name: "second-level-b",
                  depth: 0,
                  [...]
              }
  
          ]
      }
  ]
  </code></pre>
  
  <p>The question is, is this even possible to achieve using MongoDB in combination with mongoose or do we need to sort the result with JS.</p>