【发布时间】:2021-04-07 08:13:22
【问题描述】:
我有 mongoDB 集合,其中每个文档看起来像:
{
"_id": 1,
"name": "Aurelia Menendez",
"scores": [{
"score": 60.06045071030959,
"type": "exam"
}, {
"score": 52.79790691903873,
"type": "quiz"
}, {
"score": 71.76133439165544,
"type": "homework"
}]
}
我试着跑:
db.students.mapReduce(
function() {
emit(this._id, this.scores.map(a => a.score));
},
function(_id, values) {
//here i try:
1) return values.reduce((a, b) => a + b);
2) return values.reduce((a, b) => a + b, 0);
3) return Array.sum(values);
},
{ out: "total_scores" }
)
我得到了什么?我得到每个文档的集合:
- “值”是数组:
{
"_id": 20,
"value": [42.17439799514388, 71.99314840599558, 81.23972632069464]
}
- “价值”就是价值
{
"_id": 188,
"value": "060.314725741828,41.12327471818652,74.8699176311771"
}
- “值”是数组
{
"_id": 193,
"value": [47.67196715489599, 41.55743490493954, 70.4612811769744]
}
为什么我没有得到元素的总和?当我尝试使用this.scores 或this.scores.score 而不是this.scores.map(a => a.score) 时,我拥有所有属性或空值。
也许有人知道,我做错了什么?
【问题讨论】:
标签: javascript mongodb mapreduce