title: Get paths from directed graph structure using MongoDB
body: <p><strong>Background</strong></p>
  
  <p>I have a mongodb database with documents containing a title and a list of titles of other documents it has connections to. That list of other titles contains titles which do not (yet) exist in the database. For some kind of a recommendation and finding equalities I need to know how "far" two documents are from each other and especially how I can get from one to another document.</p>
  
  <p>Ideally I'd like to have a solution for which I don't need to know the whole database in my program which would make this problem kind of trivial.</p>
  
  <p><img src="https://i.imgur.com/nEbzfJ2.png" alt="image"></p>
  
  <p><strong>What I tried</strong></p>
  
  <p>I tried using <code>$graphLookup</code> in aggregation but I feel like it will not be able to provide me with a generic solution top this. Using $graphLookup I was able to get Element A and all its connected Elements (B, D) "populated", but the result was very rough and looked very brute-forcish.</p>
  
  <p><strong>Expected result</strong></p>
  
  <p>I would like to know all possible paths (maybe with a max-depth) from one element to another.</p>
  
  <p>Based on the image above, for query <code>"A -&gt; B"</code> I'd like to get a list of <code>"A, D, B"</code> and <code>"A, B"</code>.</p>
  
  <p>For <code>"A -&gt; G"</code> I'd like to get <code>"A, B, F, G"</code>, <code>"A, B, G"</code> and <code>"A, D, B, G"</code>.</p>
  
  <p>Sadly, I do have a lot of elements (thousands). Is there some smart query I can use to maybe get the exact result using a query only? Or atleast some smart filter query to get only a subset of all relevant documents which I can then combine with graph theory in python or node.js?</p>