title: Mongoose / MongoDB: count elements in array
body: <p>I'm trying to count the number of occurrences of a string in an array in my collection using Mongoose.  My "schema" looks like this:</p>
  
  <pre><code>var ThingSchema = new Schema({
    tokens: [ String ]
  });
  </code></pre>
  
  <p>My objective is to get the top 10 "tokens" in the "Thing" collection, which can contain multiple values per document.  For example:</p>
  
  <pre><code>var documentOne = {
      _id: ObjectId('50ff1299a6177ef9160007fa')
    , tokens: [ 'foo' ]
  }
  
  var documentTwo = {
      _id: ObjectId('50ff1299a6177ef9160007fb')
    , tokens: [ 'foo', 'bar' ]
  }
  
  var documentThree = {
      _id: ObjectId('50ff1299a6177ef9160007fc')
    , tokens: [ 'foo', 'bar', 'baz' ]
  }
  
  var documentFour = {
      _id: ObjectId('50ff1299a6177ef9160007fd')
    , tokens: [ 'foo', 'baz' ]
  }
  </code></pre>
  
  <p>...would give me data result:</p>
  
  <pre><code>[ foo: 4, bar: 2 baz: 2 ]
  </code></pre>
  
  <p>I'm considering using MapReduce and Aggregate for this tool, but I'm not certain what is the best option.</p>