Analysis of total Sum function and return format inconsistency in MongoDB through MapReduce

Source: Internet
Author: User
Tags emit

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.