title: MongoDB aggregation: get counts of key, value pairs
body: <p>I have a collection that has documents structured like so:</p>
  
  <pre><code>{
    responses: {
      key1: bar,
      key2: quux
      ...
    },...
  }
  </code></pre>
  
  <p>Is there a way to get the counts of each unique key, value pair in the <code>responses</code> object? For example, I'd like to see results like the following (the exact structure doesn't matter):</p>
  
  <pre><code>{
    key1: {
      foo: 7 // aka 7 objects are similar to { responses: [{key: foo}] }
      bar: 30
    },
    key2: {
      baz: 24,
      quux: 13
    }
  }
  </code></pre>