title: Group By Map Key and find sum of count field - Mongodb 3.6.9
body: <p>I have a document in below structure.</p>
  <pre><code>[{
          &quot;_id&quot;: 0001,
          &quot;data&quot;: {
              &quot;status&quot;: {
                  &quot;COMPLETED&quot;: [{
                          &quot;count&quot;: 1.0,
                          &quot;assignee&quot;: &quot;ABC&quot;
                      }, {
                          &quot;count&quot;: 1.0,
                          &quot;assignee&quot;: &quot;DEF&quot;
                      }
                  ],
                  &quot;OPEN&quot;: [{
                          &quot;count&quot;: 5.0,
                          &quot;assignee&quot;: &quot;XYZ&quot;
                      }, {
                          &quot;count&quot;: 5.0,
                          &quot;assignee&quot;: &quot;PQR&quot;
                      }
                  ]
              },
              &quot;assignee&quot;: {
                  &quot;User1&quot;: [{
                          &quot;count&quot;: 1.0,
                          &quot;status&quot;: &quot;OPEN&quot;
                      }
                  ],
                  &quot;User2&quot;: [{
                          &quot;count&quot;: 5.0,
                          &quot;status&quot;: &quot;COMPLETED&quot;
                      }
                  ]
              }
          }
      }, {
          &quot;_id&quot;: 0002,
          &quot;data&quot;: {
              &quot;status&quot;: {
                  &quot;COMPLETED&quot;: [{
                          &quot;count&quot;: 10.0,
                          &quot;assignee&quot;: &quot;AAA&quot;
                      }, {
                          &quot;count&quot;: 10.0,
                          &quot;assignee&quot;: &quot;BBB&quot;
                      }
                  ],
                  &quot;OPEN&quot;: [{
                          &quot;count&quot;: 50.0,
                          &quot;assignee&quot;: &quot;CCC&quot;
                      }, {
                          &quot;count&quot;: 50.0,
                          &quot;assignee&quot;: &quot;DDD&quot;
                      }
                  ]
              },
              &quot;assignee&quot;: {
                  &quot;User1&quot;: [{
                          &quot;count&quot;: 10.0,
                          &quot;status&quot;: &quot;OPEN&quot;
                      }
                  ],
                  &quot;User2&quot;: [{
                          &quot;count&quot;: 15.0,
                          &quot;status&quot;: &quot;COMPLETED&quot;
                      }
                  ]
              }
          }
      }
  ]
  </code></pre>
  <p>How do we find the total count when grouped by each field.
  For example -</p>
  <pre><code>group by &quot;status&quot; should give result as;  
     =&gt; COMPLETED = 22,   OPEN = 110
  
  group by &quot;assignee&quot; should give result as;  
     =&gt; User1 = 11 ,  User2 = 20
  </code></pre>
  <p>we can simplify the doc by moving &quot;status&quot; and &quot;assignee&quot; out of &quot;data&quot; if required.</p>