title: MongoDB aggregation graphlookup child's parent
body: <p>I've got some documents representing folders. I'd like to match every folder's parent if it has one.</p>
  <pre><code>{&quot;_id&quot;:{&quot;$oid&quot;:&quot;5f8c406fc8c88110e0d927f2&quot;},&quot;name&quot;:&quot;A&quot;}
  {&quot;_id&quot;:{&quot;$oid&quot;:&quot;5f8c406fc8c88110e0d927f3&quot;},&quot;parentFolderId&quot;:&quot;5f8c406fc8c88110e0d927f2&quot;,&quot;name&quot;:&quot;B&quot;}
  </code></pre>
  <p>Even if the parent-child relationships are deeper, for <strong>A</strong> I need an <strong>empty parent folder</strong>, and for <strong>B</strong> <strong>parent folder A</strong>. I tried to use GraphLookup for this but it's not working (every <code>parentFolder</code> is empty), and I don't know what I'm missing:</p>
  <pre><code>db.folders.aggregate([
      {
          $addFields: {
              convertedId: { $toString: &quot;$_id&quot; }
          }
      },
      {
          $graphLookup: {
              from: &quot;folders&quot;,
              startWith: &quot;$parentFolderId&quot;,
              connectFromField: &quot;parentFolderId&quot;,
              connectToField: &quot;convertedId&quot;,
              as: &quot;parentFolder&quot;,
              maxDepth: 0,
          }
      }
  ])
  </code></pre>
  <p>If I flip the <code>startWith</code>, <code>connectFromField</code>, <code>connectToFields</code> respectively, I get the <em>reverse</em> of what I need, A has an array which is containing B, B has nothing.</p>
  <p>Edit:</p>
  <p>With the non-working lookup, I get the following result:</p>
  <pre><code>{ &quot;_id&quot; : ObjectId(&quot;5f8c406fc8c88110e0d927f2&quot;), &quot;name&quot; : &quot;A&quot;, &quot;convertedId&quot; : &quot;5f8c406fc8c88110e0d927f2&quot;, &quot;parentFolder&quot; : [ ] }
  { &quot;_id&quot; : ObjectId(&quot;5f8c406fc8c88110e0d927f3&quot;), &quot;parentFolderId&quot; : &quot;5f8c406fc8c88110e0d927f2&quot;, &quot;name&quot; : &quot;B&quot;, &quot;convertedId&quot; : &quot;5f8c406fc8c88110e0d927f3&quot;, &quot;parentFolder&quot; : [ ] }
  </code></pre>
  <p>What I'd need is something like:</p>
  <pre><code>{ &quot;_id&quot; : ObjectId(&quot;5f8c406fc8c88110e0d927f2&quot;), &quot;name&quot; : &quot;A&quot;, &quot;convertedId&quot; : &quot;5f8c406fc8c88110e0d927f2&quot;, &quot;parentFolder&quot; : [ ] }
  { &quot;_id&quot; : ObjectId(&quot;5f8c406fc8c88110e0d927f3&quot;), &quot;parentFolderId&quot; : &quot;5f8c406fc8c88110e0d927f2&quot;, &quot;name&quot; : &quot;B&quot;, &quot;convertedId&quot; : &quot;5f8c406fc8c88110e0d927f3&quot;, &quot;parentFolder&quot; : [ { &quot;_id&quot; : ObjectId(&quot;5f8c406fc8c88110e0d927f2&quot;), &quot;name&quot; : &quot;A&quot;, &quot;convertedId&quot; : &quot;5f8c406fc8c88110e0d927f2&quot; } ] }
  </code></pre>
  <p>The only difference I see from the official &quot;reportsTo&quot; example that I'm using the ID field to match the documents, but that shouldn't be a problem, since I'm not using the ObjectID: <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/" rel="nofollow noreferrer">https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/</a></p>