我按organization
分組,並使用$addToSet
顯示與該organization
相關的不同machineIds
。我想得到的計數machineIds
。但是,下面的代碼返回所有machineIds
的計數,而不是不同的計數。有沒有另外一種方法可以獲得唯一的總數machineIds
?Mongodb - 使用addToSet的項目數
db.getCollection('newcollections').aggregate([{
$group: {
_id: {
organization: "$user.organization"
},
machineId: {
"$addToSet": "$user.machineId"
},
count: {
$sum: 1
}
}
}])
你能告訴你的文件嗎? – styvane
「_id」:{ 「組織」: 「」 }, 「設備ID」: 「」 ], 「計數」:2.0000000000000000 – ellier
這個例子中只有一個設備ID,但數爲2 – ellier