title: Mongoose/Mongodb Aggregate - group and average multiple fields
body: <p>I have a Post model with 2 fields : date and rating. How would I go about getting an average aggregate rating for each date? So group by date first and then average the rating across all posts for that date. I need to do this within mongoose but their docs are so difficult to understand.</p>
  <pre><code>const PostSchema = new Schema({
      date: {
        type: String,
        default: getToday() //this is just a new Date() formatted
      },
      rating: {
        type: Number,
        required: true
      }
    }, 
  ) 
  </code></pre>
  <p>This gives me the average across all dates but I can't figure out how to filter it by date:</p>
  <pre><code>Post.aggregate([
      { $group: { _id: null, avgRating: { $avg: '$rating' }}}
    ])
    .then(function (res) {
      console.log(res[0][&quot;avgRating&quot;]); 
    })
  </code></pre>