I have a document related to the role or designations of employees. This is the document structure.
{
\"_id\" : ObjectId(\"5660c2a5b6fcba2d47baa2d9\"),
You have to use $group
instead:
db.employee_role.aggregate(
{
$group: {
_id: "$role_title",
total: { $sum: { $size:"$employees" } }
}
}
)
You group by role_title
and then, you add the number of employees.
You could use this:
db.employee_role.aggregate(
[
{
$match: { role_title: "Carpenter" }
},
{
$group: {
_id: "$role_title",
total: { $sum: { $size: "$employees"} }
}
}
]
)