title: How to merge documents on a grouping condition and create a new field in mongo collection?
body: <p>It is a data migration in a mongo db collection. </p>
  
  <p>It is a data migration in a mongo db collection. 
  In the code below, I have some documents. 
  On the basis of common field1, all the documents should be grouped and merged to a single document. In the new merged document I want a new field4 which is actually a list of field2 and field3 objects from the group. And field2 and field3 fields should not be there in the single merged document. </p>
  
  <p>In this way my document count would be reduced in the collection on the basis of unique field1.</p>
  
  <p><em>Document 1</em>
  <em>{
      "field1" : "e8b0f2b5-de4a-4449-b2aa-15f009ac4978",
      "field2" : "9911",
      "field3" : "a"
  }</em></p>
  
  <p><em>Document 2</em>
  <em>{
      "field1" : "e8b0f2b5-de4a-4449-b2aa-15f009ac4978",
      "field2" : "1100",
      "field3" : "b"
  }</em></p>
  
  <p><em>Document 3</em>
  <em>{
      "field1" : "e8b0f2b5-de4a-4449-b2aa-15f009ac4978",
      "field2" : "0099",
      "field3" : "c"
  }</em></p>
  
  <h1>Expected outcome</h1>
  
  <p>should be 1 document in a format </p>
  
  <p><em>{<br>
     "field1":"e8b0f2b5-de4a-4449-b2aa-15f009ac4978",
     "field4":[<br>
        {<br>
           "field2":"9911",
           "field3":"a"
        },
        {<br>
           "field2":"1100",
           "field3":"b"
        },
        {<br>
           "field2":"0099",
           "field3":"c"
        }
     ]
  }</em></p>