0
我正在學習Mongodb,並試圖從Mongodb實現堆疊的highcharts。我正在努力顯示數據以匹配highcharts文檔。這是我迄今所做的:Mongodb + Stacked Highcharts
MongoDB的文件:
{
"_id" : ObjectId("576a.....b"),
"to: "Jane",
"mode" : "Open"
}
{
"_id" : ObjectId("576a.....c"),
"to: "Jane",
"mode" : "Closed"
}
{
"_id" : ObjectId("576a.....d"),
"to: "Smith",
"mode" : "Open"
}
MongoDB的骨料:
db.collection.aggregate([
{
$group : {
_id : {"to" : "$to", "mode": "$mode"},
y : {$sum : 1}
}
}, {
$project : {
"to" : "$_id.to",
"mode" : "$_id.mode",
"_id" : 0,
"y" : "$y"
}
}, {
$group : {
_id : {"to" : "$to" },
items : {$push : {item : "$mode", qty: "$y"}}
}
}
])
我得到的輸出:
[{
"_id" : {
"to" : "Jane"
},
"items" : [{
"item" : "Open",
"qty" : 1
}, {
"item" : "Closed",
"qty" : 1
}]
}, {
"_id" : {
"to" : "Smith"
},
"items" : [{
"item" : "Open",
"qty" : 1
}]
}
}]
相反,我想要以下輸出顯示高位圖:
categories = ["Jane","Smith"]
series = [{
name : 'Open',
data: [1,1] //1 -open jane, 1- open smith
}, {
name : 'Closed',
data : [1,0] //1 - closed jane, 0- closed smith
}]
任何想法如何獲得此輸出?謝謝!
如果誰可以幫我MongoDB的聚集,以獲得預期的結果? – nair
不能用'categories ='和'series ='得到結果但是你可以用'map'或'forEach'方法創建所描述的結構。 https://docs.mongodb.com/manual/reference/method/cursor.map/ – AstraSerg