我一直在开发中使用mongodb 2.0.4,在部署到生产时我没有意识到它们运行的是2.2.0。我使用的mapReduce函数不再像2.0.4那样表现,我无法弄清楚原因。
mongodb 2.0.4(注意:M,F,T,I,C,H,R,D应该总共144,在本例中也是如此):
{
"_id" : "",
"value" : {
"tag" : "",
"networth" : 43558505,
"land" : 201837,
"alive" : 144,
"M" : 86,
"F" : 6,
"T" : 5,
"I" : 10,
"C" : 17,
"H" : 4,
"R" : 12,
"D" : 4,
"gdi" : 15
}
}
mongo 2.2.0(m + f + t + i + c + h + r + d总计最多108,当它应该总计144)
{
"_id" : "",
"value" : {
"tag" : "",
"networth" : 43558505,
"land" : 201837,
"alive" : 144,
"M" : 67,
"F" : 5,
"T" : 3,
"I" : 6,
"C" : 13,
"H" : 3,
"R" : 9,
"D" : 2,
"gdi" : 15
}
}
这是我正在使用的map / reduce函数:
// Map function
var map = function() {
var key = this.tag;
var value =
{
tag: this.tag,
networth: this.networth,
land: this.land,
alive: this.alive,
gdi: this.gdi,
gov: this.gov
};
emit(key, value);
};
减少功能
var reduce = function(k,vals) {
reducedVals = { tag: k, networth: 0, land: 0, alive: 0, M: 0, F: 0, T: 0, I: 0, C: 0, H: 0, R: 0, D: 0, gdi: 0 };
for (var i = 0; i < vals.length; i++){
reducedVals.networth += vals[i].networth;
reducedVals.land += vals[i].land;
reducedVals.alive += vals[i].alive;
reducedVals.gdi += vals[i].gdi;
if (vals[i].gov == "M") reducedVals.M = reducedVals.M + 1;
if (vals[i].gov == "F") reducedVals.F = reducedVals.F + 1;
if (vals[i].gov == "T") reducedVals.T = reducedVals.T + 1;
if (vals[i].gov == "I") reducedVals.I = reducedVals.I + 1;
if (vals[i].gov == "C") reducedVals.C = reducedVals.C + 1;
if (vals[i].gov == "H") reducedVals.H = reducedVals.H + 1;
if (vals[i].gov == "R") reducedVals.R = reducedVals.R + 1;
if (vals[i].gov == "D") reducedVals.D = reducedVals.D + 1;
}
return reducedVals;
};
执行map reduce
collection.mapReduce(map, reduce, {out: {replace : 'alliances'}, query: {"alive":1}}, function(err, collection) {
// Mapreduce returns the temporary collection with the results
db.close();
});
如此简短的概述..该集合有一堆分数,采用以下格式:
"alive" : 1,
"countryNumber" : 47,
"deleted" : 0,
"gdi" : 0,
"gov" : "C",
"land" : 20111,
"name" : "AtheistCelebratingXmas",
"networth" : 9793082,
"protection" : 0,
"rank" : 1,
"resetid" : 407,
"serverid" : 9,
"tag" : "Evolve",
"vacation" : 0
我基本上按tag
进行分组,总结了networth
,land
和alive
列。然后检查gov
列的值并总结D,R等的总和。是否有一个特殊的原因我错过了为什么这在2.2 vs 2.0.4中无法正常工作?无论哪种方式,新的聚合命令都会更容易地做到这一点吗?我简要介绍了一下,并且可以通过tag
获取该组,networth
和alive
列的总和可以使用 - 但不知道从哪里开始列。
答案 0 :(得分:1)
emit
函数中map
对象的形状必须与reduce
函数返回的对象相同。这是因为当MongoDB决定并行化map-reduce时,reduce
调用的结果可以反馈到reduce
。
因此,您需要更改map
以将值组合为emit
,这样它们的结构与reduce
返回的结果相同:
var value = {
"tag" : this.tag,
"networth" : this.networth,
"land" : this.land,
"alive" : this.alive,
"gdi" : this.gdi
};
value[this.gov] = 1;
然后相应地更新reduce
功能。
答案 1 :(得分:0)
我接受了JohnnyHK的回答,因为他回答了为什么我的代码无法从一个版本到另一个版本;但是,我觉得我应该在代码中发布我修改的内容来解决问题。
地图功能:
var map = function() {
var key = this.tag;
var fields = {
tag: this.tag,
networth: this.networth,
land: this.land,
alive: this.alive,
gdi: this.gdi,
M: 0,
F: 0,
T: 0,
I: 0,
C: 0,
H: 0,
R: 0,
D: 0
};
if (this.gov == "M") fields["M"] = 1
else fields["M"] = 0
if (this.gov == "F") fields["F"] = 1
else fields["F"] = 0
if (this.gov == "T") fields["T"] = 1
else fields["T"] = 0
if (this.gov == "I") fields["I"] = 1
else fields["I"] = 0
if (this.gov == "C") fields["C"] = 1
else fields["C"] = 0
if (this.gov == "H") fields["H"] = 1
else fields["H"] = 0
if (this.gov == "R") fields["R"] = 1
else fields["R"] = 0
if (this.gov == "D") fields["D"] = 1
else fields["D"] = 0
emit(key, fields);
};
减少功能:
var reduce = function(k,vals) {
reducedVals = { tag: k, networth: 0, land: 0, alive: 0, M: 0, F: 0, T: 0, I: 0, C: 0, H: 0, R: 0, D: 0, gdi: 0};
for (var i = 0; i < vals.length; i++){
reducedVals.networth += vals[i].networth;
reducedVals.land += vals[i].land;
reducedVals.alive += vals[i].alive;
reducedVals.gdi += vals[i].gdi;
reducedVals.M += vals[i].M;
reducedVals.F += vals[i].F;
reducedVals.T += vals[i].T;
reducedVals.I += vals[i].I;
reducedVals.C += vals[i].C;
reducedVals.H += vals[i].H;
reducedVals.R += vals[i].R;
reducedVals.D += vals[i].D;
}
return reducedVals;
};