title: Model tree structure with aggregation in MongoDb
body: <p>I want to aggregate a model tree structure directly in mongodb database with Aggregations.</p>
  
  <p>Is it possible to do hierarchical aggregations like that ? Currently, I do that in a program.</p>
  
  <p>I want to use a collection like :</p>
  
  <pre><code>{
    "Name" : "john",
    "Parents" : ["sandy", "bryan"]
  }
  
  {
    "Name" : "sandy",
    "Parents" : ["bill", "daisy"]
  }
  
  {
    "Name" : "bryan",
    "Parents" : ["dora", "david"]
  }
  
  {
    "Name" : "dora",
    "Parents" : ["cliff", "darla"]
  }
  </code></pre>
  
  <p>And generate a new collection like :</p>
  
  <pre><code>{
    "Name" : "sandy",
    "Parents" : ["bill", "daisy"],
    "Ancestrors" : ["bill", "daisy"]
  }
  
  {
    "Name" : "dora",
    "Parents" : ["cliff", "darla"],
    "Ancestrors" : ["cliff", "darla"]
  }
  
  {
    "Name" : "bryan",
    "Parents" : ["dora", "david"],
    "Ancestrors" : ["dora", "david", "cliff", "darla"]
  }
  
  {
    "Name" : "john",
    "Parents" : ["sandy", "bryan"],
    "Ancestrors" : ["sandy", "bryan", "bill", "daisy", "dora", "david", "cliff", "darla"]
  }
  </code></pre>