如何操作我的mongoose文档以匹配charts.js格式

时间:2016-07-13 20:55:36

标签: javascript node.js mongodb mongoose

我有以下JSON数据;

    playerBase = [
      {
        "_id": {
          "year": 2016,
          "month": 7,
          "day": 13,
          "hour": 20
        },
        "playerCount": 44.04424778761062
      },
      {
        "_id": {
           "year": 2016,
          "month": 7,
          "day": 13,
          "hour": 19
        },
        "playerCount": 1
      },
      {
        "_id": {
          "year": 2016,
          "month": 7,
          "day": 13,
          "hour": 18
        },
        "playerCount": 1
      }
    ]

我使用以下聚合命令

生成了这个
    var query = [{serverID:server._id,name:"PlayerCount"}]
     ConversionPoint.aggregate(
    {
     $match: {$and: query}
     },{ 
     $ group:{
       _id : {
        year:{$year:'$timestamp'},
        month:{$month:'$timestamp'},
        day:{$dayOfMonth:'$timestamp'},
        hour:{$hour:'$timestamp'}
     },
     playerCount: {$avg:'$data.playerCount'}
   }
 },callback
)

我希望能够按摩数据,并以这种格式进行处理;

    var data = {
        labels: ["7/13/2016 18:00", "7/13/2016 19:00", "7/13/2016 20:00"],
        datasets: [
            {
                label: "Playerbase",
                data: [1,1,44],
            }
        ]
    };

我调查了$ group和$ project但我发现它相当混乱,因为我没有花太多时间在MongoDB或mongoose上。

0 个答案:

没有答案