title: MongoDb $graphLookup get root in result
body: <p>I have the following collection</p>
  
  <pre><code>[
      {
        "_id": "1",
        "name": "A",
        "parent": "",
  
      },
      {
        "_id": "2",
        "name": "B",
        "parent": "A",
  
      },
      {
        "_id": "3",
        "name": "C",
        "parent": "B",
  
      },
      {
        "_id": "4",
        "name": "D",
        "parent": "C",
  
      }
    ]
  </code></pre>
  
  <p>I'm trying to get a response like this:</p>
  
  <pre><code>[
    {
      "_id": "4",
      "name": "D",
      "parent": "C"
    },
    {
      "_id": "3",
      "name": "C",
      "parent": "B"
    },
    {
      "_id": "2",
      "name": "B",
      "parent": "A"
    },
    {
      "_id": "1",
      "name": "A",
      "parent": ""
    }
  ]
  </code></pre>
  
  <p>I've tried several things but I got stuck last try is :</p>
  
  <pre><code>db.collection.aggregate([
    {
      "$match": {
        "name": "D"
      }
    },
    {
      $graphLookup: {
        from: "collection",
        startWith: "$parent",
        connectFromField: "parent",
        connectToField: "name",
        as: "chain"
      }
    },
    {
      $facet: {
        "chain": [
          {
            $project: {
              "chain": 1,
              "_id": 0
            }
          },
  
        ],
        "root": [
          {
            $project: {
              "chain": 0
            }
          },
  
        ]
      },
  
    },
    {
      $unwind: "$chain"
    },
    {
      $unwind: "$root"
    }
  ])
  </code></pre>
  
  <p>The result is from my try  is:</p>
  
  <pre><code>[
    {
      "chain": {
        "chain": [
          {
            "_id": "1",
            "name": "A",
            "parent": ""
          },
          {
            "_id": "3",
            "name": "C",
            "parent": "B"
          },
          {
            "_id": "2",
            "name": "B",
            "parent": "A"
          }
        ]
      },
      "root": {
        "_id": "4",
        "name": "D",
        "parent": "C"
      }
    }
  ]
  </code></pre>
  
  <p>I would like to push that "root" element in the chain array and make that array the top result then sort it. In my real use case the "parent" and the "name" are just some ID's so I can't just sort alphabetically  (I would be calling this from java so then I can make the sorting there, if not possible from mongo).</p>
  
  <p>I've tried to use $push in a projection to merge them but according to the documentation that doesn't work, $push only works in $group if I got that right.</p>