title: MongoDB query distinct in subdocuments
body: <p>I'm using Mongoose with NodeJS (typescript). 
  I'm trying to sum the count per location. Example output :</p>
  
  <pre><code>[ 
   { name : "Bronx", count : 6 }, 
   { name : "Brooklyn", count : 6 }, 
   { name : "Manhattan", count : 6 }, 
   { name : "Queens", count : 6 } 
  ]
  </code></pre>
  
  <blockquote>
    <p>Current data model:</p>
  </blockquote>
  
  <pre><code>data:
  [
      {
          "news": {
              "_id": "5c7615a4ef5238a6c47cbcb9",
              "locations": [
                  {
                      "_id": "5c7615a4ef5238a6c47cbcc6",
                      "id": "1",
                      "name": "Manhattan",                        
                      "children": [
                          {
                              "_id": "5c7615a4ef5238a6c47cbcc8",
                              "count": 3
                          },
                          {
                              "_id": "5c7615a4ef5238a6c47cbcc7",
                              "count": 2
                          }
                      ]
                  }
              ]
          }
      },
      { 
       .... 
      }
  
  ]
  </code></pre>
  
  <p>The last query that I build was :</p>
  
  <pre><code>DataModel.aggregate([
  { "$unwind": "$data.news.locations" },
  {
      "$group": {
          "_id": "$data.news.locations",
          "count": { "$sum": "$$data.news.locations.zipcodes.count" }
      }
  }]).exec(function(err, results){
      if (err) throw err;
          console.log(JSON.stringify(results, null, 4));
  
       }); 
  </code></pre>
  
  <p>But I'm new handle queries in MongoDB with Mongoose, so any help I really appreciate. thanks.</p>