title: MongoDB aggregation sum values and group by property
body: <p>I have a collection that looks like this</p>
  <pre><code>{
   balance: string
   symbol: string
   address: string
  }
  </code></pre>
  <p>I want to query that collection using an aggregation as following:</p>
  <pre><code>input array addresses[]
  </code></pre>
  <p>query collection where collection.address contained in provided address Array and SUM all balances grouped by symbol.</p>
  <p>The desired output should look like this:</p>
  <pre><code>[
    {symbol: xy, balance: summedBalanceForThisSymbol},
    {symbol: yz, balance: summedBalanceForThisSymbol},
  ]
  </code></pre>
  <p>I am rather new to mongoDB, this is what I have which doesnt work at all.</p>
  <pre><code>  const pipe = [
          {
              match: [
                  {
                      address: { $in: addresses } //addresses is an array of string values
                  }
              ]
          },
          {
              group: {
                  objectId: &quot;$symbol&quot;,
                  balance: { $sum: &quot;$balance&quot; }
              }
          }
      ]
  </code></pre>
  <p>I have to use objectId instead of _id because the SDK I am using requires to do so. I also need the string balance values to be converted to a number (I guess double) before summing up.</p>