簡體   English   中英

在Google圖表中顯示mongodb結果

[英]display mongodb result in a google chart

我有一些商店的單擊項目的mongodb集合。 如果有人單擊某個項目,則數據庫會以以下格式添加json:

{ 
"_id" : { "$oid" : "56cf06ce0b7707c83e5a8c64" }, 
"date" : { "$date" : 1456408270811 }, 
"name" : "Jura whisky", 
"store" : "56c9bfed942693bb745229ef" 
}

因此,數據庫具有不同的項目,不同的存儲日期也不同。 如果要獲得特定商店所有項目的所有點擊,請使用以下查詢:

var id = global.actuallUser._doc._id;
            id = id.toString();
            global.databaseStatistic.collection(global.ARTICLEPRESS).aggregate(
                    [
                        {$match: {
                                $and: [
                                    {
                                        store: id,
                                        date: {
                                            $gte: new Date('12/01/2013'),
                                            $lte: new Date('12/01/2017')
                                        }
                                    }
                                ]
                            }},
                        {$group: {"_id": {name: "$name", day: {$dayOfMonth: "$date"},month: {$month: "$date"},year: {$year: "$date"}}, "count": {$sum: 1}}}
                    ]
                    ).toArray(function (err, result) {
                res.send(result);
            });

現在,我得到一個具有名稱,天...屬性的對象數組。是否可以這種格式獲得此結果:

['date', 'name', 'count'],
['1.1.2000', Jura whisky, 400],
['1.1.2000', Potato, 460],
['1.1.2000', Pizza, 1120],
['2.1.2000', Jura whisky, 540]

因此,我可以使用Google Chart API創建數據圖表。

謝謝!

假設您由於ajax調用而得到results JSON對象。

var columns = Object.keys(results[0]);
var data = results.map(function (result) {
  var tableRow = [];
  columns.forEach(function (col) {
    tableRow.push(result[col]);
  });
  return tableRow;
});

data將包含要由Google圖表呈現的表格行。

var dataTable = new google.visualization.DataTable();
columns.forEach(function (columnName) {
  dataTable.addColumn(columnName);
});

dataTable.addRows(data);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM