title: How to find most common value for specific categories in MongoDB?
body: <p>I have a dataset in MongoDB that looks like this:</p>
  
  <pre><code>{ "name": "Tom's", "category": "coffee shop" },
  { "name": "Red Lobster", "category": "restaurant" },
  { "name": "Tom's", "category": "coffee shop" },
  { "name": "Starbucks", "category": "coffee shop" },
  { "name": "Central Park", "category": "park" },
  { "name": "Office", "category": "office" },
  { "name": "Red Lobster", "category": "restaurant" },
  { "name": "Home", "category": "home" },
  { ... } // and so on
  </code></pre>
  
  <p><strong>How can I find the most common value for specific categories?</strong> For example, the most common occurring value for <code>coffee shop</code> and <code>restaurant</code> should be Tom's and Red Lobster, respectively.</p>
  
  <p>My current <code>$aggregate</code> query only seems to list the most common occurring value among ALL of the dataset:</p>
  
  <pre><code>db.collection.aggregate(
  { "$group": { "_id": { "name": "$name" }, "count": { "$sum":1 } }}, 
  { "$group": { "_id": "$_id.name", "count": { "$sum": "$count" } }}, 
  { "$sort": { "count":-1 }}
  )
  </code></pre>