title: MongoDB group by array inner-elements
body: <p>I've got a list of articles, and each of them has an array property which lists various individuals mentioned in them:</p>
  
  <pre><code>_id: {
      $oid: "52b632a9e4f2ba13c82ccd23"
  },
  providerName: "The Guardian",
  url: "http://feeds.theguardian.com/c/34708/f/663860/s/3516cebc/sc/38/l/0L0Stheguardian0N0Cmusic0C20A130Cdec0C220Cwaterboys0Efishermans0Eblues0Etour0Ehammersmith/story01.htm",
  subject: "The Waterboys – review",
  class_artist: [
      "paul mccartney"
  ]
  </code></pre>
  
  <p>I've been trying (unsuccessfully) to get a list of all the individual artists (<code>class_artist</code>), based on the number of articles they've been tagged in within the past 7 days.</p>
  
  <p>I've gotten as far as:</p>
  
  <pre><code>var date = new Date();
  date.setDate(date.getDate() - 7);
  
  db.articles.group({
      key: { class_artist: 1 },
      cond: { class_date: { $gt: date } },
      reduce: function ( curr, result ) { result.cnt++; },
      initial: { cnt : 0 }
  }).sort({cnt: -1});
  </code></pre>
  
  <p>But unfortunately, it doesn't count them based on the individual array values, but by array compositions (that is, lists of artists).</p>
  
  <p>I tried using the <code>$unwind</code> function, but have not been able to make it work.</p>