我一直在开发中使用 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;
};
执行地图缩减
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
进行分组。然后检查列的值并总结 D、R 等的总数。有没有什么特别的原因我错过了为什么这在 2.2 和 2.0.4 中不能正常工作?无论哪种方式,新的聚合命令会更容易做到这一点吗?我给了它一个简短的查看,并且可以得到 group by , sums for和columns 工作——但不知道从哪里开始 gov 列。networth
land
alive
gov
tag
networth
alive