title: Mongo building a family tree
body: <p>I am trying to build a family tree with potentially infinite levels of parents and children.  I will also want to find brothers, sisters, cousins, etc. and am a bit confused at the approach of building an array in Javascript when the data only gives parents of every person.</p>
  
  <p>In a MongoDB Collection called "Users" I have the following entries</p>
  
  <pre><code>{ id: 1, name: "Target", parents: [3,4] }
  { id: 2, name: "Wife" }
  { id: 3, name: "Dad", parents: [5,6] }
  { id: 4, name: "Mom" }
  { id: 5, name: "Dads Dad", parents: [7,8] }
  { id: 6, name: "Dads Mom" }
  { id: 7, name: "Dads Dads Dad", parents: 9 }
  { id: 8, name: "Dads Dads Mom" }
  { id: 9, name: "Dads Dads Dads Dad" }
  { id: 10, name: "Son", parents: [1, 2] }
  { id: 11, name: "Sons Son", parents: [10] }
  { id: 12, name: "Sons Sons Son", parents: [11] }
  { id: 13, name: "Brother", parents: [3,4] }
  { id: 14, name: "Brothers Son", parents: [13] }
  { id: 15, name: "Uncle", parents: [5,6] }
  { id: 16, name: "Aunt", parents: [5,6] }
  { id: 17, name: "Daughter", parents: [5,6] }
  </code></pre>
  
  <p>I can easily loop every child using the ids and output it but it will not give structure of children of children just loops and outputs.</p>
  
  <pre><code>getChildren = function(id) {
      var children = Users.find({parents: id});
      children.forEach(function(child) {
          console.log(child);
          getChildren(child.id);
      });
  };
  </code></pre>
  
  <p>I have been trying to create two global variables <code>ascendants</code> and <code>descendants</code> so that I could loop parents, grandparents, etc. and nest their children and childen's children inside that (for brothers and then nephews, etc). and then similar with descendents.  This has turned complex because of having multiple nests and having to be able to add an entry inside another entry.</p>
  
  <p>Was hoping someone could help me with structuring this. So I could get something that could be built into an HTML/CSS family tree.  To get non-children/parents I would probably have to conditionally loop again?</p>
  
  <pre><code>descendents = {
      10: {           // son
          11: {       // grandson
              12: {}  // great grandson
          }
      },
      17: {           // daughter
  
      }
  }
  </code></pre>