0

我一直在开发中使用 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 列。networthlandalivegovtagnetworthalive

4

2 回答 2

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相应地更新您的功能。

顺便说一句,如果有足够的文档,这在 2.0.4 中也会失败。只是 2.2 对何时并行化使用了不同的阈值。

于 2013-01-03T21:31:07.857 回答
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;

};
于 2013-01-05T05:18:01.673 回答