title: mapReduce/Aggregation: Group by a value in a nested document
body: <p>imagine I have a collection like this:</p>
  
  <pre><code>{
    "_id": "10280",
    "city": "NEW YORK",
    "state": "NY",
    "departments": [
               {"departmentType":"01",
                "departmentHead":"Peter"},
               {"departmentType":"02",
                "departmentHead":"John"}
    ]
  },
  {
    "_id": "10281",
    "city": "LOS ANGELES",
    "state": "CA",
    "departments": [
               {"departmentType":"02",
                "departmentHead":"Joan"},
               {"departmentType":"03",
                "departmentHead":"Mary"}
    ]
  },
  {
    "_id": "10284",
    "city": "MIAMI",
    "state": "FL",
    "department": [
    "departments": [
               {"departmentType":"01",
                "departmentHead":"George"},
               {"departmentType":"02",
                "departmentHead":"Harry"}
    ]
  }
  </code></pre>
  
  <p>I'd like to get a count per departmentType, something like:</p>
  
  <pre><code>[{"departmentType":"01", "dCount":2},
   {"departmentType":"02", "dCount":3},
   {"departmentType":"03", "dCount":1}
  ]
  </code></pre>
  
  <p>For this, I've tried almost everything already, but all examples I find online are easier ones where the group by is done over a field at the root level of the document. Instead, here I'm trying to group by departmentType, and that seems to break everything I found so far.</p>
  
  <p>Any ideas on how to do this using Mongoose's aggregation implementation or mapreduce?</p>
  
  <p>Ideally, I'd like to exclude all departmentTypes with count &lt;= 1 and sort the results by departmentType.</p>
  
  <p>Thank you all in advance!</p>