title: Multiple group operations using Mongo aggregation framework
body: <p>Given a set of questions that have linked survey and category id:</p>
  
  <pre><code>&gt; db.questions.find().toArray();
  [
      {
          "_id" : ObjectId("4fda05bc322b1c95b531ac25"),
          "id" : 1,
          "name" : "Question 1",
          "category_id" : 1,
          "survey_id" : 1,
          "score" : 5
      },
      {
          "_id" : ObjectId("4fda05cb322b1c95b531ac26"),
          "id" : 2,
          "name" : "Question 2",
          "category_id" : 1,
          "survey_id" : 1,
          "score" : 3
      },
      {
          "_id" : ObjectId("4fda05d9322b1c95b531ac27"),
          "id" : 3,
          "name" : "Question 3",
          "category_id" : 2,
          "survey_id" : 1,
          "score" : 4
      },
      {
          "_id" : ObjectId("4fda4287322b1c95b531ac28"),
          "id" : 4,
          "name" : "Question 4",
          "category_id" : 2,
          "survey_id" : 1,
          "score" : 7
      }
  ]
  </code></pre>
  
  <p>I can find the category average with:</p>
  
  <pre><code>db.questions.aggregate(
      { $group : {
          _id : "$category_id",
          avg_score : { $avg : "$score" }
      }
  }
  );
  
  {
      "result" : [
          {
              "_id" : 1,
              "avg_score" : 4
          },
          {
              "_id" : 2,
              "avg_score" : 5.5
          }
      ],
      "ok" : 1
  }
  </code></pre>
  
  <p><strong>How can I get the average of category averages (note this is different than simply averaging all questions)?</strong>  I would assume I would do multiple group operations but this fails:</p>
  
  <pre><code>&gt; db.questions.aggregate(
  ...   { $group : {
  ...     _id : "$category_id",
  ...     avg_score : { $avg : "$score" },
  ...   }},
  ...   { $group : {
  ...     _id : "$survey_id",
  ...     avg_score : { $avg : "$score" },
  ...   }}
  ... );
  {
      "errmsg" : "exception: the _id field for a group must not be undefined",
      "code" : 15956,
      "ok" : 0
  }
  &gt;
  </code></pre>