title: Mongodb Aggregate using $group twice
body: <p>I have a bunch of documents in mongo with the following structure:</p>
  
  <pre><code>{
      "_id" : "",
      "number" : 2,
      "colour" : {
          "_id" : "",
          "name" : "Green",
          "hex" : "00ff00"
      },
      "position" : {
          "_id" : "",
          "name" : "Defence",
          "type" : "position"
      },
      "ageGroup" : {
          "_id" : "",
          "name" : "Minor Peewee",
          "type" : "age"
      },
      "companyId" : ""
  }
  </code></pre>
  
  <p>I'm currently using Mongo's aggregate to group the documents by ageGroup.name which returns:</p>
  
  <pre><code>//Query
  Jerseys.aggregate([
    {$match: { companyId: { $in: companyId } } },
    {$group: {_id: "$ageGroup.name", jerseys: { $push: "$$ROOT" }} }
  ]);
  
  //returns
  {
     _id: "Minor Peewee",
     jerseys: array[]
  }
  </code></pre>
  
  <p>but I'd like it to also group by position.name within the age groups. ie:</p>
  
  <pre><code>{
    _id: "Minor Peewee",
    positions: array[]
  }
  //in positions array...
  {
    _id: "Defence",
    jerseys: array[]
  }
  // or ageGroups-&gt;positions-&gt;jerseys if that makes more sense.
  </code></pre>
  
  <p>I've tried multiple groups but I don't think I'm setting them up correctly I always seem to get an array of _id's. I'm using Meteor as the server and I'm doing it within a meteor method.</p>