title: How to aggregate and group in mongoose
body: <p>I have lot of accounts with each of them having an employee assigned. I want to find the number of accounts of each employee. How do I do this task using aggregate of mongoose(mongodb). I am familiar with other functions of mongoose and able to achieve with following code</p>
  
  <pre><code>exports.accountsOfEachEmployee = function(req, res) {
    Account.find({active:true}).exec(function(err, accounts){
      if (err || !accounts) res.status(400).send({
          message: 'could not retrieve accounts from database'
        });
      var accountsOfEachEmployee = {};
      for (var i = 0; i &lt; accounts.length; i++) {
        if(accountsOfEachEmployee[order[i].employee]) {
          accountsOfEachEmployee[order[i].employee] = 1;
        } else {
          accountsOfEachEmployee[order[i].employee]++;
        }
      }
      res.json(accountsOfEachEmployee);
    }); 
  };
  </code></pre>
  
  <p>Is using aggregate faster? How does grouping and aggregation work in mongoose(mongodb). Following is my schema of accounts</p>
  
  <pre><code>var AccountSchema = new Schema({
    active: {
      type : Boolean, 
      default: false
    },
    employee: {
      type: Schema.ObjectId,
      ref: 'Employee'
    },
  });
  </code></pre>