Mongodb 用于打印线图的Mapreduce帮助

Mongodb 用于打印线图的Mapreduce帮助,mongodb,mapreduce,sails.js,Mongodb,Mapreduce,Sails.js,我正在从事SAILSJS项目,从下面的数据生成图形折线图数据时遇到一些问题 [{ "_id" : "575fcb020d28bbc117647c7a", "childid" : "575a4952bfb2ad01481e9060", "starttime" : "1465895783", "endtime" : "1465895783", "word" : "TOE", "gamescore" : "1", "createdAt" : "2016-06-14T09:14:4

我正在从事SAILSJS项目,从下面的数据生成图形折线图数据时遇到一些问题

   [{   "_id" : "575fcb020d28bbc117647c7a",  "childid" : "575a4952bfb2ad01481e9060",  "starttime" : "1465895783",  "endtime" : "1465895783",  "word" : "TOE",  "gamescore" : "1",  "createdAt" : "2016-06-14T09:14:42.959Z"
    },
    { "_id" : "575fcbd150c93cf819faecfe",  "childid" : "575a4952bfb2ad01481e9060", "starttime" : "1465895983",  "endtime" : "1465895990",  "word" : "SLOW",   "gamescore" : "1",   "createdAt" : "2016-06-14T09:18:09.453Z"
    },
    {"_id" : "575fcbd150c93cf819faecff", "childid" : "575a4952bfb2ad01481e9060","starttime" : "1465895959",  "endtime" : "1465895959",  "word" : "GLOW",  "gamescore" : "1",  "createdAt" : "2016-06-14T09:18:09.454Z"
    },
    { "_id" : "57619851d71451e56949dd4d",  "starttime" : "1466013832",  "gamescore" : "1", "childid" : "5761973fd71451e56949dd3c",  "endtime" : "1466013850",  "word" : "YUM",  "createdAt" : "2016-06-15T18:02:57.543Z"
    },
    { "_id" : "576198ead71451e56949dd58",  "starttime" : "1466014023",  "gamescore" : "1",  "childid" : "5761973fd71451e56949dd3c",  "endtime" : "1466014030",  "word" : "BELT",  "createdAt" : "2016-06-15T18:05:30.945Z"
    },
    {  "_id" : "57619915d71451e56949dd5f", "childid" : "576197c2d71451e56949dd40",  "starttime" : "1466013984",  "endtime" : "1466013989",  "word" : "PLAY",  "gamescore" : "1",  "createdAt" : "2016-06-15T18:06:13.388Z"
    },
   { "_id" : "57619958d71451e56949dd65",  "starttime" : "1466014140",  "gamescore" : "1",  "childid" : "5761973fd71451e56949dd3c",  "endtime" : "1466014143",  "word" : "BELL",  "createdAt" : "2016-06-15T18:07:20.298Z"
      }............]
我正在寻求帮助,以使时间间隔为4小时,为一天和分组的游戏分数正确是“1”和游戏分数错误是“0”以下类似的东西

对于下面的输出,我使用聚合来获得两个日期之间每天4小时的时间间隔。但若并没有特定时期的游戏分数记录,那个么就不会创建该时期的数据点,因为我希望数据点和gmscore都是0,反映在那个时期

两个日期每天4次间隔的预期输出

   [{ "txnTime" : ISODate("2016-06-10T04:00:00.000Z"),"gmScoreCorrectCount" : 15, "gmScoreWrongCount" : 2 },
   { "txnTime" : ISODate("2016-06-10T08:00:00.000Z"),"gmScoreCorrectCount" : 10, "gmScoreWrongCount" : 8 },
   { "txnTime" : ISODate("2016-06-13T04:00:00.000Z"),"gmScoreCorrectCount" : 9, "gmScoreWrongCount" : 9 },
   { "txnTime" : ISODate("2016-06-14T04:00:00.000Z"),"gmScoreCorrectCount" : 7, "gmScoreWrongCount" : 8 },
   { "txnTime" : ISODate("2016-06-14T08:00:00.000Z"),"gmScoreCorrectCount" : 6, "gmScoreWrongCount" : 7 }]
我对使用mapreduce非常陌生,但我知道它可以通过mapreduce实现:

    mapf = function () { 
// round down to nearest hour
d = this.createdAt;
d.setMinutes(0);
d.setSeconds(0);
d.setMilliseconds(0);
emit(d, this.gamescore); 
 }

   reducef = function (key, values) {
var sum = 0;
for (var v in values) {
    sum += values;
}
return sum;
  }

     db.activity.mapReduce(mapf, reducef, {out: { merge : "hourly_logs" }})

首先,请允许我注意,您的样本输入与您给出的样本输出不一致

因此,使用map reduce解决问题的可能解决方案可以是:

mapf = function () { 

    d = new Date(this.createdAt);

    // bucket every 4 hours
    d.setHours(d.getHours()-(d.getHours()%4));

    // remove minutes, seconds and milis from date
    d.setMinutes(0);
    d.setSeconds(0);
    d.setMilliseconds(0);

    gscore = parseInt(this.gamescore);

    gmScoreCorrectCount = 0;
    gmScoreWrongCount = 0;
    if(gscore > 0){
        gmScoreCorrectCount += 1;
    }else{
        gmScoreWrongCount += 1;
    }

    emit(d, {"gmScoreCorrectCount": gmScoreCorrectCount, "gmScoreWrongCount": gmScoreWrongCount});
}

reducef = function (key, values) {

    var gmScoreCorrectCount = 0;
    var gmScoreWrongCount = 0;

    for (var i=0; i<values.length; i++) {
        v = values[i]
        gmScoreCorrectCount += v['gmScoreCorrectCount'];
        gmScoreWrongCount += v['gmScoreWrongCount'];

    }
    return {"gmScoreCorrectCount": gmScoreCorrectCount, "gmScoreWrongCount": gmScoreWrongCount};
}
mapf=function(){
d=新日期(this.createdAt);
//每4小时一桶
d、 设置小时数(d.getHours()-(d.getHours()%4));
//从日期中删除分钟、秒和毫秒
d、 设置分钟(0);
d、 设置秒(0);
d、 设置毫秒(0);
gscore=parseInt(this.gamescore);
GmscorrectorCount=0;
gmscoreErrorCount=0;
如果(gscore>0){
GMC计数+=1;
}否则{
gmscoreErrorCount+=1;
}
发出(d,{“gmscorectorcount”:gmscorectorcount,“gmscoreErrorCount”:gmscoreErrorCount});
}
reducef=函数(键、值){
var gmScoreCorrectCount=0;
var GMScoreErrorCount=0;
对于(变量i=0;i 1){
//按创建的结果排序
res.sort(函数(a,b){
返回parseFloat(a._id)-parseFloat(b._id);
});
correctResult=[res[0]];
referenceDate=res[0][''u id'];
对于(var ri=1;ri 14400000){
numberOfMissing=Math.ceil((差异-14400000)/14400000);
//注入缺少的值

对于(var ni=1;ni首先,让我注意您的样本输入与您给出的样本输出不一致

因此,使用map reduce解决问题的可能解决方案可以是:

mapf = function () { 

    d = new Date(this.createdAt);

    // bucket every 4 hours
    d.setHours(d.getHours()-(d.getHours()%4));

    // remove minutes, seconds and milis from date
    d.setMinutes(0);
    d.setSeconds(0);
    d.setMilliseconds(0);

    gscore = parseInt(this.gamescore);

    gmScoreCorrectCount = 0;
    gmScoreWrongCount = 0;
    if(gscore > 0){
        gmScoreCorrectCount += 1;
    }else{
        gmScoreWrongCount += 1;
    }

    emit(d, {"gmScoreCorrectCount": gmScoreCorrectCount, "gmScoreWrongCount": gmScoreWrongCount});
}

reducef = function (key, values) {

    var gmScoreCorrectCount = 0;
    var gmScoreWrongCount = 0;

    for (var i=0; i<values.length; i++) {
        v = values[i]
        gmScoreCorrectCount += v['gmScoreCorrectCount'];
        gmScoreWrongCount += v['gmScoreWrongCount'];

    }
    return {"gmScoreCorrectCount": gmScoreCorrectCount, "gmScoreWrongCount": gmScoreWrongCount};
}
mapf=function(){
d=新日期(this.createdAt);
//每4小时一桶
d、 设置小时数(d.getHours()-(d.getHours()%4));
//从日期中删除分钟、秒和毫秒
d、 设置分钟(0);
d、 设置秒(0);
d、 设置毫秒(0);
gscore=parseInt(this.gamescore);
GmscorrectorCount=0;
gmscoreErrorCount=0;
如果(gscore>0){
GMC计数+=1;
}否则{
gmscoreErrorCount+=1;
}
发出(d,{“gmscorectorcount”:gmscorectorcount,“gmscoreErrorCount”:gmscoreErrorCount});
}
reducef=函数(键、值){
var gmScoreCorrectCount=0;
var GMScoreErrorCount=0;
对于(变量i=0;i 1){
//按创建的结果排序
res.sort(函数(a,b){
返回parseFloat(a._id)-parseFloat(b._id);
});
correctResult=[res[0]];
referenceDate=res[0][''u id'];
对于(var ri=1;ri 14400000){
numberOfMissing=Math.ceil((差异-14400000)/14400000);
//注入缺少的值

对于(var ni=1;ni Hi@joao,谢谢你的输入,首先我为我糟糕的英语道歉。上面给出的答案是正确的,但部分原因是,这里的主要问题是,我需要4小时的间隔,不管“createdAt”,因为如果在该时间间隔内没有可用的数据,上面的答案将不会给出。我如何才能克服这一点。正在寻找您的答复。@chridam,请仔细查看。@AyyappaA要添加缺少的值,您需要自己将其注入客户端。在map reduce步骤中无法执行该操作。@AyyappaA我添加了一些示例代码,说明如何在ect javascript中缺少的值。干杯。感谢您的回复。joao。嗨@joao,感谢您的输入,首先我为我糟糕的英语道歉。上面生成的答案是正确的,但部分是,这里的主要问题是,我需要4小时的间隔,不管“createdAt”,因为如果在该时间间隔内没有可用的数据,上面的答案将不会给出。我如何才能克服这一点。正在寻找您的答复。@chridam,请仔细查看。@AyyappaA要添加缺少的值,您需要自己将其注入客户端。在map reduce步骤中无法执行该操作。@AyyappaA我添加了一些示例代码,说明如何在ect javascript中缺少的值。干杯。感谢您的回复,我的一天。joao。