title: MongoDB/Mongoose: Using $sum to calculate quantity when names are matching
body: <p>I have some data within a mongodb collection which looks like this:</p>
  <pre><code>[
          {
                  &quot;name&quot; : &quot;Apple&quot;,
                  &quot;quantity&quot; : &quot;4&quot;,
            
          },
          {
                  &quot;name&quot; : &quot;Apple&quot;,
                  &quot;quantity&quot; : &quot;6&quot;,
                  
          },
          {
                  &quot;name&quot; : &quot;Orange&quot;,
                  &quot;quantity&quot; : &quot;2&quot;,
          },
          {
                  &quot;name&quot; : &quot;Orange&quot;,
                  &quot;quantity&quot; : &quot;3&quot;,
                  
          },
  ]
  </code></pre>
  <p>I am trying to figure out a mongodb query and then its mongoose counterpart where I could utilize $sum to get all unique names with their respective sum. So the correct output after the query should look like this:</p>
  <pre><code>[
   {
     name: &quot;Apple&quot;,
     totalQuantity: &quot;10&quot;
   },
   {
     name: &quot;Orange&quot;,
     totalQuantity: &quot;5&quot;
   }
  </code></pre>