簡體   English   中英

日期范圍內的MongoDB MapReduce標准偏差

[英]MongoDB MapReduce Standard Deviation over date range

下午好,我想知道是否有人可以幫助我。 我目前正在研究使用MongoDB聚合框架和MapReduce函數。

我的數據集看起來像這樣

[{
     "Name" : "Person 1",
    "RunningSpeed" : [{
            "Date" : ISODate("2005-07-23T23:00:00.000Z"),
            "Value" : 10
        }, {
            "Date" : ISODate("2006-07-23T23:00:00.000Z"),
            "Value" : 20
        }, {
            "Date" : ISODate("2007-07-23T23:00:00.000Z"),
            "Value" : 30
        }, {
            "Date" : ISODate("2008-07-23T23:00:00.000Z"),
            "Value" : 40
        }

    ]

}, {
    "Name" : "Person 2",
    "RunningSpeed" : [{
            "Date" : ISODate("2005-07-23T23:00:00.000Z"),
            "Value" : 5
        }, {
            "Date" : ISODate("2006-07-23T23:00:00.000Z"),
            "Value" : 10
        }, {
            "Date" : ISODate("2007-07-23T23:00:00.000Z"),
            "Value" : 20
        }, {
            "Date" : ISODate("2008-07-23T23:00:00.000Z"),
            "Value" : 40
        }

    ]

}, {
    "Name" : "Person 3",
    "RunningSpeed" : [{
            "Date" : ISODate("2005-07-23T23:00:00.000Z"),
            "Value" : 20
        }, {
            "Date" : ISODate("2006-07-23T23:00:00.000Z"),
            "Value" : 10
        }, {
            "Date" : ISODate("2007-07-23T23:00:00.000Z"),
            "Value" : 30
        }, {
            "Date" : ISODate("2008-07-23T23:00:00.000Z"),
            "Value" : 25
        }

    ]

}

]

我已經進行了很多研究,並且據我所知,尚無開箱即用的SD計算支持。 我查看了一些鏈接和SO帖子,並提出了該URL https://gist.github.com/RedBeard0531/1886960 ,這似乎是我想要的。

關於背景,我想做的就是每年生成一張SD圖表。

當前功能並不僅僅考慮整個年度的價值。 我已經將map函數更改為,並且不知道在何處放置組日期函數。

function map() {
    emit(1, // Or put a GROUP BY key here
     {sum: this.RunningSpeed.value, // the field you want stats for
      min: this.RunningSpeed.value,
      max: this.RunningSpeed.value,
      count:1,
      diff: 0, // M2,n:  sum((val-mean)^2)
});

}

但是我只能得到零。 有人可以幫助我調整此功能嗎?

您需要在每個RunningSpeed條目中使用getFullYear()和forEach循環:

function map() {
   this.RunningSpeed.forEach(function(data){
      var year = data.Date.getFullYear();
      emit(year, // Or put a GROUP BY key here, the group by key is Date.getFullYear()
          {sum: data.Value, // the field you want stats for
           min: data.Value,
           max: data.Value,
           count: 1,
           diff: 0, // M2,n: sum((val-mean)^2)
          });
   });
}

暫無
暫無

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

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