1
我有移動設備的集合,我想統計每個製造商的設備,也計算每個模型。所有我能得到迄今:MongoDB聚合與雙金額
{ $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}]}