title: Sailsjs MongoDB Aggregation Query
body: <p>I'm trying to do an aggregation query to the db for objects with a specific <code>id</code>, and sum it all up and as well give me an average.</p>
  
  <p><strong>My Scenario</strong></p>
  
  <p>I've a model that looks like this:</p>
  
  <pre><code>attributes: {
  
      user: {
          model: 'user',
          required: true
      },
  
      rating: {
          type: 'integer',
          enum: [0, 1, 2, 3, 4, 5],
          defaultsTo: 0,
          required: true
      },
  
      inventory: {
          model: 'subItem'
      },
  
      size: {
          model: 'size',
          required: true
      },
  
      isDeleted: {
          type: 'boolean',
          defaultsTo: false
      }
  }
  </code></pre>
  
  <p>Aggregating for the average rating like this:</p>
  
  <pre><code>const aggQuery = [
      {
          $match: {"size" : theRating.data[0].size, "isDeleted": false}
      },
      {
          $group: {
              _id: "$size",
              total: { $sum: 1 },
              average: { $avg: "$rating" }
          }
      }
  ]
  </code></pre>
  
  <p><strong>My Function</strong></p>
  
  <pre><code>module.exports = {
  
      aggregateQuery: (model, aggregate) =&gt; {
          return new Promise((resolve, reject) =&gt; {
  
              model.native(function (err, collection) {
                  if (err) return reject(err);
  
                  collection.aggregate(aggregate, function (err, result) {
                      if (err) return reject(err);
  
                      return resolve(result);
                  })
              })
          })
      }
  }
  </code></pre>
  
  <p><strong>Use Case</strong>
  I populate the db with 16 records while writing the test, but, after running the aggregate query, I get an empty array</p>
  
  <pre><code>NativeQueryService.aggregateQuery(Rating, aggQuery).then(result =&gt; {
      console.log(result)
  }).catch(err =&gt; {
      console.log(err)
  })
  </code></pre>
  
  <p>Any help on what I'm doing wrong would be appreciated. Thanks.</p>