MongoDB mapreduce learning notes

Source: Internet
Author: User

Mapreduce should be complicated in MongoDB operations. I started to understand it and moved my mind, so it was recorded here!

Command syntax: For details, see

 
DB. runcommand ({mapreduce: String, Set Name, MAP: function, see reduce: function, see [, query: Document, transition document before sending to map function] [, sort: Document, sort documents before sending to the map function] [, limit: integer, maximum number of documents sent to the map function] [, out: String, set of statistical results to be saved] [, keeptemp: boolean value. indicates whether the temporary result set is saved when the link is closed.] [, finalize: function, send the result of reduce to this function for final processing] [, scope: Document, JSCodeVariable] [, jsmode: Boolean value, whether to reduce the bson and JS conversions during execution, true by default] // note: if false, bson --> JS --> map --> bson --> JS --> reduce --> bson can process very large mapreduce jobs,
// If it is set to true, bson --> JS --> map --> reduce --> bson [, verbose: Boolean value; Whether to generate more detailed server logs; default value: True]});

Test data:

Now I want to count the name of the same age, that is, the result as follows:

{Age: 0, names: ["name_6", "name_12", "name_18"]}
{Age: 1, names: ["name_1", "name_7", "name_13", "name_19"]}
......

The first step is to write the map function, which can be simply understood as a group ~

 
VaRM =Function(){
Emit (This. Age,This. Name );
}

The first parameter of emit is the key, which is the basis of grouping. this parameter is the age, and the last parameter is the value, which can be the data to be calculated, value can be a JSON object.
In this way, M will group the sent data according to the key, which can be considered as the following structure:

 
Group 1
{Key: 0, values: ["name_6", "name_12", "name_18"]

Group 2
{Key: 1, values: ["name_1", "name_7", "name_13", "name_19"]
......

The key in the group is actually the value of age. values is an array, and all the Members in the array have the same age !!.

Step 2 is simplified. Compile the reduce function:

VaRR =Function(Key, values ){
VaRRet = {age: Key, names: Values };
ReturnRET;
}

The reduce function processes each group, and the parameters are exactly the keys and values in the group we imagine.

Here, the reduce function simply wraps the key and values, because we don't have to deal with the desired result, and then return an object. The object structure exactly matches what we think! :

 
{Age: corresponding age, names: [name 1, name 2 ..]}

Finally, you can write the finalize function to final the return values of reduce:

 
VaRF =Function(Key, rval ){
If(Key = 0 ){
Rval. MSG = "a new life, baby! ";
}
ReturnRval
}

Here, the key is the preceding key, that is, the age, and rval is the return value of Reduce. Therefore, an rval instance is {age: 0, names: ["name_6 ", "name_12", "name_18"]},

Check whether the key is 0. If the key is 0 and the MSG attribute is added to the rval object, you can also check that rval. Age = 0, because the key and rval. Age are equal !!

The other options are not mentioned here.

Run:

 
DB. runcommand ({
Mapreduce: "T ",
Map: m,
Reduce: R,
Finalize: F,
Out: "t_age_names"
}
)

The result is imported to the t_age_names set. The query result is exactly what I want. Looking at the structure of the document, it is not difficult to find that _ id is the key, and value is the returned value after processing.

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.