title: count array occurrences across all documents with mongo
body: <p>Im trying to pull data on a collection of documents which looks like:</p>
  
  <pre><code>[
    {
      name: 'john',
      sex: 'male',
      hobbies: ['football', 'tennis', 'swimming']
    },
    {
      name: 'betty'
      sex: 'female',
      hobbies: ['football', 'tennis']
    },
    {
      name: 'frank'
      sex: 'male',
      hobbies: ['football', 'tennis']
    } 
  ]
  </code></pre>
  
  <p>I am trying to use the aggregation framework to present the data, split by sex, counting the most common hobbies. The results should look something like.</p>
  
  <pre><code>{ _id: 'male', 
    total: 2, 
    hobbies: {
      football: 2,
      tennis: 2,
      swimming: 1
    } 
  },
  { _id: 'female', 
    total: 1, 
      hobbies: {
        football: 1,
        tennis: 1
      } 
  }
  </code></pre>
  
  <p>So far I can get the total of each sex, but i'm not sure how I could possibly use unwind to get the totals of the hobbies array.</p>
  
  <p>My code so far:</p>
  
  <pre><code>collection.aggregate([
          { 
              $group: { 
                  _id: '$sex', 
                  total: { $sum: 1 }
              }
          }
      ])
  </code></pre>