title: How to $match and then accumulate within $group stage in MongoDB aggregation
body: <p>In one aggregation stage within my MongoDB view pipeline I am using <code>$group</code>. One of the things I'm doing within that <code>$group</code> stage is summing up an <code>openBalance</code>, like this:</p>
  
  <pre><code>  "openBalance": {
      "$sum": "$openBalance"
    },
  </code></pre>
  
  <p>This works. </p>
  
  <p>However, I also need to get another value, that first matches to a condition, and <em>then</em> sums data. In other words, something like this:</p>
  
  <pre><code>  "customerResponsibleOpenBalance": {
      "$match": {
        "customerResponsible": true
      },
      "$sum": "$openBalance"
    },
  </code></pre>
  
  <p>However, I can't do this like this within a <code>$group</code> stage, because it results in this error:</p>
  
  <blockquote>
    <p><code>customerResponsibleOpenBalance</code> must specify one accumulator.</p>
  </blockquote>
  
  <p>So how would I go about doing this in a different way? Are <code>$facets</code> my only option here? </p>