2014-10-07 34 views
1

下午好,所以我想知道有沒有人能幫助我。我目前正在研究使用MongoDB Aggregation Framework和MapReduce函數。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圖表。

目前的功能並沒有考慮每年僅作爲一個整體的價值。我已經改變了地圖功能,並且不知道在哪裏放置組日期功能。

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) 
}); 

}

不過,我只是得到零。有人可以幫我調整這個功能嗎?

+0

如果要組上一年你所需要的'emit'鍵(1約GROUP BY旁邊的評論對它)是一年或類似的東西,將按年分組的結果。 – wdberkeley 2014-10-08 16:41:41

回答

0

你需要通過每個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) 
      }); 
    }); 
} 
+0

謝謝,for循環就是我所缺少的 – N00b3eva 2014-11-01 18:46:06