title: MongoDB double $group aggregation
body: <p>I have several documents that looks like this:</p>
  
  <pre><code>{
          "_id" : ObjectId("50b59cd75bed76f46522c34e"),
          "player_id" : 0,
          "league_id" : 2,
          "results" : [
                  {   "discipline" : "football",
                      "score" : 25.15
                  },
                  {
                          "discipline" : "basketball",
                          "score" : 21.24
                  },
                  {
                          "discipline" : "cycling",
                          "score" : 68.19
                  },]
  }
  </code></pre>
  
  <p>I try to aggregate this data. First <strong>unwind</strong> results array, then leave only "<strong>football</strong>" and "<strong>cycling</strong>", next count <strong>average</strong> result. This part I did, and it is working.
  My code:</p>
  
  <pre><code>db.grades.aggregate(
      {$unwind:"$results"},
      {$match: {$or: [{"results.discipline":"football"},{"results.discipline":"cycling"} ]}},
      {$group:{_id:{player_id:"$player_id",league_id:"$league_id"}, 'average':{$avg:"$results.score"}}},
      )
  </code></pre>
  
  <p>Then I try to aggregate by <strong>league_id</strong>, it means, average players results in specific leagues, add to code above:
  <code>{$group:{_id:"$_id.league_id",aver_league:{$avg:$average}}}</code></p>
  
  <p>And now code looks like:</p>
  
  <pre><code>db.grades.aggregate(
          {$unwind:"$results"},
          {$match: {$or: [{"results.discipline":"football"},{"results.discipline":"cycling"} ]}},
          {$group:{_id:{player_id:"$player_id",league_id:"$league_id"}, 'average':{$avg:"$results.score"}}},
  {$group:{_id:"$_id.league_id",aver_league:{$avg:$average}}}
          )
  </code></pre>
  
  <p>Console displays: <code>JavaScript execution failed: ReferenceError: $average is not defined.</code> What is wrong? Where did I make a mistake? Is it possible to aggregate by <code>_id.league_id</code>?</p>