title: Mongodb query for 2 level grouping
body: <p>Suppose I have documents that, among others, have these fields:</p>
  
  
  
  <pre class="lang-js prettyprint-override"><code>{
      "class" : String,
      "type" : String,
      "name" : String,
  }
  </code></pre>
  
  <p>For example, many like this:</p>
  
  <pre class="lang-js prettyprint-override"><code>{
      "class": "class A",
      "type": "type 1",
      "Name": "ObjectA1"
  }
  
  {
      "class": "class A",
      "type": "type 2",
      "Name": "ObjectA2_1"
  }
  
  {
      "class": "class A",
      "type": "type 2",
      "Name": "ObjectA2_2"
  }
  
  {  
      "class": "class B ",
      "type": "type 3",
      "Name": "ObjectB3"
  }
  </code></pre>
  
  <p>What I want is a query that returns me the following structure</p>
  
  <pre class="lang-js prettyprint-override"><code>{
      "class A" : {
          "type 1" : ["ObjectA1"],
          "type 2" : ["ObjectA2_1", "ObjectA2_2"]
      },
      "class B" : {
          "type 3" : ["ObjectB3"]
      } 
  }
  </code></pre>
  
  <p>I tried using aggregate with $group but could not do this. Any thoughts?</p>
  
  <p>PS: I would like to do this on mongodb shell, not mongoose or something like this.</p>