1
나는 모바일 장치 모음을 가지고 있으며 각 제조업체의 장치 수를 계산하고 각 모델 수를 계산하려고합니다. 내가 지금까지 얻을 수있는 모든은 다음과 같습니다MongoDB aggregate with dual sum
{ $group : {
_id : "$hw.man",
mantotal : { $sum : 1 },
models : { $addToSet : "$hw.mod" }
} }
result:
[{"_id":"LGE","mantotal":1,"models":["GT540"]},{"_id":"asus","mantotal":1,"models":["Nexus 7"]},{"_id":"samsung","mantotal":3,"models":["GT-I9300","GT-I9200"]}]
또는
{ $group : {
_id : { man : "$hw.man", mod : "$hw.mod" },
total : { $sum : 1 } }
}
result:
[{"_id":{"man":"LGE","mod":"GT540"},"total":1},{"_id":{"man":"asus","mod":"Nexus 7"},"total":1},{"_id":{"man":"samsung","mod":"GT-I9300"},"total":2},{"_id":{"man":"samsung","mod":"GT-I9200"},"total":1}]
어떻게이 같은 결과를 얻을 수 있습니다 : 당신이 전화를 나타내는 문서의 간단한 컬렉션이 가정
{"_id":"samsung","mantotal":3,"models":[{mod: "GT-I9300", modtotal: 2}, {mod: "GT-I9200", modtotal: 1}]}