Establish the following test data to count the number of students in each class and their scores through mapreduce.
The code is as follows:
Public stringSumstudentscore () {varCollection = _database.getcollection ("Studentinfo"); //Group statistics by Class (class), and the number of records (1) and scores (this) for each record. Score) as the reduce parameter stringMapfunction =@"function () {emit (this). Class,{count:1,score:this. Score}); };"; //Note that the values here are one to many records after grouping stringReducefunction =@"function (class,values) {var reduced = {sumcount:0,sumscore:0}; Values.foreach (function (val) { Reduced. Sumcount + = val. Count; Reduced. Sumscore + = val. Score; }); return reduced; }"; stringOutputinfo =string. Empty; varresult =collection. MapReduce (Mapfunction, reducefunction); foreach(varIteminchresult. GetResults ()) {Outputinfo+ = Item. ToString () +Environment.NewLine; } returnOutputinfo;}
The results of the implementation are as follows:
{"_id": "Class 11", "value": {"Sumcount": 2.0, "Sumscore": 9.0}}
{"_id": "Class 12", "value": {"Sumcount": 2.0, "Sumscore": 8.0}}
{"_id": "Class 13", "value": {"Count": 1.0, "Score": 5.0}}
There is no problem with the statistical results, but the return format of Class 13 in the third group is significantly different from the first two, and the use of MapReduce is due to the misuse of MapReduce, which requires the following:
The value parameter of the reduce method must be consistent with the returned result.
Then analyze the above code:
The values parameter of reduce is emit by the map method, so the parameter format is: {Count:number,score:number}, and the format of the return parameter for reduce is: {sumcount:number,sumscore: Number}, and the format is inconsistent, resulting in this problem.
Modify the above code to match the emit result with the reduce return format:
Public stringSumstudentscore () {varCollection = _database.getcollection ("Studentinfo"); //Group statistics by Class (class), and the number of records (1) and scores (this) for each record. Score) as the reduce parameter stringMapfunction =@"function () {emit (this). Class,{sumcount:1,sumscore:this. SCORE}); };"; //Note that the values here are one to many records after grouping stringReducefunction =@"function (class,values) {var reduced = {sumcount:0,sumscore:0}; Values.foreach (function (val) { Reduced. Sumcount + = val. Sumcount; Reduced. Sumscore + = val. Sumscore; }); return reduced; }"; stringOutputinfo =string. Empty; varresult =collection. MapReduce (Mapfunction, reducefunction); foreach(varIteminchresult. GetResults ()) {Outputinfo+ = Item. ToString () +Environment.NewLine; } returnOutputinfo;}
The output is correct:
{"_id": "Class 11", "value": {"Sumcount": 2.0, "Sumscore": 9.0}}
{"_id": "Class 12", "value": {"Sumcount": 2.0, "Sumscore": 8.0}}
{"_id": "Class 13", "value": {"Sumcount": 1.0, "Sumscore": 5.0}}
Resources:
About "Come, let me show you a fantastic MongoDB operation of the MapReduce!" "Explanation of
Db.collection.mapReduce ()-mongodb Manual 2.6.7
Analysis of total Sum function and return format inconsistency in MongoDB through MapReduce