title: Multiple group using Mongoose
body: <p>I'm wanting to get the sum of all events for each company. Here is my data:</p>
  
  <pre><code>{company: "1" event:"a"}
  {company: "1" event:"b"}
  {company: "1" event:"c"}
  {company: "2" event:"b"}
  {company: "2" event:"b"}
  {company: "3" event:"c"}
  {company: "3" event:"c"}
  </code></pre>
  
  <p>I currently have this to aggregate the date for events but I'm struggling to further group them by company:</p>
  
  <pre><code>{
    "aggregate": [
  
      {
        "$group": {
          "_id": "$event",
          "count": {"$sum": 1}
        }
      },
      {"$sort": {"_id": 1}}
    ]
  }
  </code></pre>
  
  <p>Which renders these results:</p>
  
  <p>[
      {
          "_id": "a",
          "count": 1
      },
      {
          "_id": "b",
          "count": 3
      },
      {
          "_id": "c",
          "count": 2
      }]</p>
  
  <p>How can I further group the event counts by company to produce something along the lines of:</p>
  
  <pre><code> [{
          "_id": {
              "company" :"1",
              "events": [
                  {
                      "event": "a",
                      "count": 1
                  },
                  {
                      "event": "b",
                      "count": 1
                  },
                  {
                      "event": "c",
                      "count": 1
                  },
              ]
          }
      },
      {
          "_id": {
              "company" :"2",
              "events": [
                  {
                      "event": "b",
                      "count": 2
                  }
              ]
          }
      },
      {
          "_id": {
              "company" :"3",
              "events": [
                  {
                      "event": "c",
                      "count": 2
                  }
              ]
          }
      }
      ]
  </code></pre>
  
  <p>I've tried all sorts of further groups in the query but can't produce the data I'm after. There is also one caveat: I'm unable to process anything server side or change the schema, so therefore I am only enable to pass in a query which I hope can be done.</p>