Mongodb mongo中的Map reduce

Mongodb mongo中的Map reduce,mongodb,mapreduce,Mongodb,Mapreduce,我有一个“匹配”类型的文档集合: { "_id": { "$oid" : "51b6eab8cd794eb62bb3e131" }, "_player1": { "_id": "2" }, "_player2": { "_id": "6" }, "_result": { "_sets": [ [ 6, 3 ], [ 6, 3 ], [ 5, 6 ], [ 6, 5 ] ] }, { "_id": { "$oid" : "51b6eab8cd794eb62bb3341" }, "_pl

我有一个“匹配”类型的文档集合:

{ 
"_id": { "$oid" : "51b6eab8cd794eb62bb3e131" }, 
"_player1": { "_id": "2" }, 
"_player2": { "_id": "6" }, 
"_result": { "_sets": [ [ 6, 3 ], [ 6, 3 ], [ 5, 6 ], [ 6, 5 ] ]
},
{ 
"_id": { "$oid" : "51b6eab8cd794eb62bb3341" }, 
"_player1": { "_id": "2" }, 
"_player2": { "_id": "3" }, 
"_result": { "_sets": [ [ 6, 3 ], [ 6, 7 ], [ 7, 6 ], [ 6, 5 ] ]
},
{ 
"_id": { "$oid" : "51b6eab8cd794eb62bb3341" }, 
"_player1": { "_id": "2" }, 
"_player2": { "_id": "4" }, 
"_result": { "_sets": [ [ 6, 1 ], [ 1, 6 ], [ 6, 2 ], [ 6, 5 ] ]
}
我需要计算一个给定Id(例如2)的玩家赢得、平局和输掉的比赛数以及赢得和输掉的分数

我尝试过这两个选项,但都不起作用(没有一个提供任何结果)

备选案文1:

matches.group({
"initial": {
"number_of_points_won": 0,
"number_of_points_lost": 0,
"number_of_matches_won": 0,
"number_of_matches_tied": 0,
"number_of_matches_lost": 0        
},
"reduce": function(obj, prev) {
    if(obj._result!=null && obj._result._sets!=null && obj._result._sets instanceof Array){
        if(obj._result._sets.lenght>0){
            current= { number_of_points_won: 0, number_of_points_lost: 0 };
             for (var idx = 0; idx < obj._sets.length; idx++) {
                if(obj._sets[idx][0] > obj._sets[idx][1]){
                    current.number_of_points_won++;
                     prev.number_of_points_won++;
                }else if(obj._sets[idx][0] < obj._sets[idx][1]){
                    current.number_of_points_lost++;
                     prev.number_of_points_lost++;
                }
            }
            if(current.number_of_points_won > current.number_of_points_lost){
                 prev.number_of_matches_won++;
             }else if(current.number_of_points_won < current.number_of_points_lost){
                 prev.number_of_matches_lost++;
            }else {
prev.number_of_matches_tied++;
            }
        }
    }
},
"cond": {
        "matches._player1._id": "2"
        }
});
matches.group({
“首字母”:{
“赢得的点数”:0,
“失去的点数”:0,
“赢得比赛的次数”:0,
“匹配的数量”:0,
“丢失的匹配数”:0
},
“减少”:功能(obj、prev){
if(obj.\u result!=null&&obj.\u result.\u sets!=null&&obj.\u result.\u sets数组实例){
如果(目标结果设置长度>0){
当前={赢得的分数为0,失去的分数为0};
对于(变量idx=0;idxobj.\u集[idx][1]){
当前。点数+赢得++;
上一次的分数为++;
}else if(obj._集[idx][0]当前失点数){
上一次比赛的次数;
}else if(当前赢得的点数<当前失去的点数){
上一个匹配项的数目丢失++;
}否则{
上一个匹配项的数量++;
}
}
}
},
“条件”:{
匹配项。玩家1。id:“2”
}
});
备选案文2:

db.matches.mapReduce(
function Map() {
var key = this._player1._id;
emit(key, {"data":[{"_sets" : this._result._sets}]}); 
},
function Reduce(key, values) {

 var reduced = {"data":[]};
for (var i in values) 
{
    var inter = values[i];
    for (var j in inter.data) {
        reduced.data.push(inter.data[j]);
    }
}

return reduced;
 }
 function Finalize(key, reduced) {
    var statistics = {"number_of_points_won": 0, "number_of_points_lost": 0, "number_of_matches_won": 0, "number_of_matches_tied":0, "number_of_matches_lost":0};
    current = { number_of_points_won: 0, number_of_points_lost: 0 };
    for (var set in reduced.data) {
       for (var idx = 0; idx < set.length; idx++) {
         if(set[idx][0] > set[idx][1]){
            current.number_of_points_won++;
            statistics.number_of_points_won++;
        }else if(set[idx][0] < set[idx][1]){
            current.number_of_points_lost++;
            statistics.number_of_points_lost++;
        }
    }
    if(current.number_of_points_won > current.number_of_points_lost){
        statistics.number_of_matches_won++;
    }else if(current.number_of_points_won < current.number_of_points_lost){
        statistics.number_of_matches_lost++;
    }else {
        statistics.number_of_matches_tied++;
       }
   }

return statistics;
 },
query : { "$where" : "{\"player1._id\" : { \"$e\" : \"2\" }}" }
);
db.matches.mapReduce(
函数映射(){
var key=此。_player1。_id;
发射(键,{“数据”:[{“{u集”:此。{u结果。{u集}]});
},
函数Reduce(键、值){
var reduced={“数据”:[]};
用于(值中的var i)
{
var inter=值[i];
用于(内部数据中的var j){
减少数据推送(内部数据[j]);
}
}
回报减少;
}
功能最终确定(键,减少){
var statistics={“赢得分数的数量”:0,“失去分数的数量”:0,“赢得比赛的数量”:0,“并列比赛的数量”:0,“失去比赛的数量”:0};
当前={赢得的分数为0,失去的分数为0};
for(在减少的数据中设置的var){
对于(var idx=0;idx设置[idx][1]){
当前。点数+赢得++;
统计数据。分数的数量为++;
}else if(设置[idx][0]<设置[idx][1]){
当前。失去的点数++;
统计。点数损失++;
}
}
如果(当前得点数>当前失点数){
统计数据。匹配的数量为++;
}else if(当前赢得的点数<当前失去的点数){
统计数据。匹配数丢失++;
}否则{
统计数据。匹配的数量++;
}
}
返回统计;
},
查询:{“$where”:“{\”玩家1.\u id\”:{\“$e\”:\“2\”}}
);

请帮忙。我是mongo的新手。

为什么不使用聚合框架?您可以使用$project“创建”您可以直接查询的字段-它肯定会比map/REDUCT(组函数是其中的一部分)运行得快得多。您好,我已经尝试过按您的方式进行操作,但无法使其工作。我可以帮您-到底是什么不起作用?经过几次测试,我在编码方面遇到了不同的问题,可能是因为我没有使用聚合框架的经验。