title: How to group over Array elements using Mongoose Aggregation FrameWork
body: <p>I have following Mongoose schemas :</p>
  
  <p><strong>EmployeeSchema :</strong></p>
  
  <pre><code>var EmployeeSchema = new Schema({
      name : String,
      employeeDetailsId: {
          type: Schema.Types.ObjectId,
          ref: 'employeedetails'
      }
  });
  </code></pre>
  
  <p><strong>EmployeeDetailSchema :</strong></p>
  
  <pre><code>var EmployeeDetailSchema = new Schema({
      employeeId: {
          type: Schema.Types.ObjectId,
          ref: 'employee'
      },
      statusId: {
          type: Schema.Types.ObjectId,
          ref: 'status'
      },
      primarySkills: [
      {
          type: Schema.Types.ObjectId,
          ref: 'skills'
      }]
  });
  </code></pre>
  
  <p><strong>SkillsSchema :</strong></p>
  
  <pre><code>var SkillsSchema = new Schema({
      name: {
          type: String,
          required: true
      }
  });
  </code></pre>
  
  <p>Following is the data that's got saved in <strong>EmployeeDetails</strong> collection :</p>
  
  <pre><code>/* 1 */
  {
      "_id" : ObjectId("583fbbfe78854dd424f0523f"),
      "employeeId" : ObjectId("583f114e1cff44b7ab414dc1"),
      "statusId" : ObjectId("583ee05a1d5161941632091a"),
      "secondarySkills" : [],
      "primarySkills" : [],
      "__v" : 0
  }
  
  /* 2 */
  {
      "_id" : ObjectId("583ff108cfa71d942269b09b"),
      "employeeId" : ObjectId("583f114e1cff44b7ab414dc4"),
      "statusId" : ObjectId("583ee05a1d5161941632091a"),
      "secondarySkills" : [],
      "primarySkills" : [],
      "__v" : 0
  }
  
  /* 3 */
  {
      "_id" : ObjectId("5848c40599fa37d40a7e7392"),
      "employeeId" : ObjectId("583f114e1cff44b7ab414dc8"),
      "secondarySkills" : [ 
          ObjectId("5838373072d7bab017488ba2")
      ],
      "primarySkills" : [ 
          ObjectId("5848c3c299fa37d40a7e7390"), 
          ObjectId("5848c3d599fa37d40a7e7391")
      ],
      "__v" : 0
  }
  
  /* 4 */
  {
      "_id" : ObjectId("5848c41699fa37d40a7e7393"),
      "employeeId" : ObjectId("583f114e1cff44b7ab414dc6"),
      "secondarySkills" : [],
      "primarySkills" : [ 
          ObjectId("5838373072d7bab017488ba2"), 
          ObjectId("5848c3c299fa37d40a7e7390"), 
          ObjectId("5848c3d599fa37d40a7e7391")
      ],
      "__v" : 0
  }
  </code></pre>
  
  <p><strong>UseCase :</strong></p>
  
  <p>When i want to group EmployeeDetails collection based on Status ID, i used following aggregation in Mongoose :</p>
  
  <pre><code>EmployeeDetailsModel.aggregate([
          {
              $group: {_id: "$statusId", count: {$sum: 1}}
          }
      ]).exec(...);
  </code></pre>
  
  <p>In similar way, i want to group based on primarySkills or secondarySkills where both of them are array of Skill ObjectID's.</p>
  
  <p>I tried few approaches but no luck. Need some help.</p>