I grouped by organization
and used $addToSet
to show the distinct machineIds
associated with that organization
. I would like to get the count of machineIds
for each organization
. However the code below is returning a count of all machineIds
, not the count of distinct ones. Is there another way to get the total unique machineIds
?
db.getCollection('newcollections').aggregate([{
$group: {
_id: {
organization: "$user.organization"
},
machineId: {
"$addToSet": "$user.machineId"
},
count: {
$sum: 1
}
}
}])
You need to use $size operator in projection like following: