title: count total number of elements inside an array in document - MongoDB
body: <p>I have a document related to the role or designations of employees. This is the document structure.</p>
  
  <pre><code>{
      "_id" : ObjectId("5660c2a5b6fcba2d47baa2d9"),
      "role_id" : 4,
      "role_title" : "Carpenter",
      "employees" : [ 
          {
              "$ref" : "employees",
              "$id" : 4,
              "$db" : "db_cw2"
          }, 
          {
              "$ref" : "employees",
              "$id" : 5,
              "$db" : "db_cw2"
          }
      ]
  },
  
  {
      "_id" : ObjectId("5660c2a5b6fcba2d47baa2d6"),
      "role_id" : 1,
      "role_title" : "Chief Executive",
      "employees" : [ 
          {
              "$ref" : "employees",
              "$id" : 1,
              "$db" : "db_cw2"
          }
      ]
  }
  </code></pre>
  
  <p>I want to write a query to show or count total number of employees who are working as 'Carpenter' or simple words count number of elements in 'employees' field where 'role_title' : 'Carpenter'.</p>
  
  <p>I have written this query but it shows total number of employees in all the documents of the collection.</p>
  
  <pre><code>db.employee_role.aggregate(
     {
          $project: { 
              count: { $size:"$employees" }
          }
     }
  )
  </code></pre>