title: MongoDB group and merge all sub documents into parent document
body: <p>Group documents and merge the list under each group to the parent document using one of the field in the sub document as key.</p>
  
  <p>Here are my documents</p>
  
  <pre><code>{ "_id" : "1", "a" : "11", "b" : "o11", "c" : "s11" }
  { "_id" : "2", "a" : "22", "b" : "o22", "c" : "s22" }
  { "_id" : "3", "a" : "11", "b" : "o12", "c" : "s12" }
  { "_id" : "4", "a" : "11", "b" : "o13", "c" : "s13" }
  { "_id" : "5", "a" : "22", "b" : "o23", "c" : "s23" }
  </code></pre>
  
  <p>I want to group by 'a' and  and merge list of subdocuments to one document like below:</p>
  
  <pre><code>{
      {a:"11", o11: { "_id" : "1", "a" : "11", "b" : "o11", "c" : "s11" }, o12: { "_id" : "3", "a" : "11", "b" : "o12", "c" : "s12" }, o13: { "_id" : "4", "a" : "11", "b" : "o13", "c" : "s13" }},
      {a:"22", o22: { "_id" : "2", "a" : "22", "b" : "o22", "c" : "s22" }, o23: { "_id" : "5", "a" : "22", "b" : "o23", "c" : "s23" }
  }
  </code></pre>