MongoDB MapReduce Learning Notes

Source: Internet
Author: User
Tags emit

MapReduce should be considered a more complex mongodb operation, I began to understand the time or moved the brain, so record here!

Command syntax: See more

db.runCommand(   { mapreduce : 字符串,集合名,     map : 函数,见下文     reduce : 函数,见下文     [, query : 文档,发往map函数前先给过渡文档]     [, sort : 文档,发往map函数前先给文档排序]     [, limit : 整数,发往map函数的文档数量上限]     [, out : 字符串,统计结果保存的集合]     [, keeptemp: 布尔值,链接关闭时临时结果集合是否保存]     [, finalize : 函数,将reduce的结果送给这个函数,做最后的处理]     [, scope : 文档,js代码中要用到的变量]     [, jsMode : 布尔值,是否减少执行过程中BSON和JS的转换,默认 true ] //注:false时 BSON-->JS-->map-->BSON-->JS-->reduce-->BSON,可处理非常大的mapreduce,<br>                                    //true时BSON-->js-->map-->reduce-->BSON     [, verbose : 布尔值,是否产生更加详细的服务器日志,默认 true ]   } );

Test data:

Now I want to count the name of the same age, which is the result like this:

{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, you can simply understand the grouping bar ~

M= () {emit (. age,.name);}

The first parameter of emit is key, which is the basis of grouping, which is naturally age, the latter is value, can be the data to be counted, the following will indicate that value can be a JSON object.
So M will send the data sent in accordance with the key group, you can imagine the following structure:

The first group {key:0,values: ["Name_6", "Name_12", "name_18"] the second group {key:1,values: ["Name_1", "name_7", "name_13", "name_19"] ...

The key in the group is actually the value of age, values are arrays, and the members of the array have the same age!!。

The second step is to simplify and write the reduce function:

R= (key,values) {ret={age:key,names:values}; RET;}

The reduce function handles each grouping, and the parameters are exactly the key and values in the group we imagined.

In this case, the reduce function simply wraps the key and values, because we don't have to deal with the result we want, and then we return an object. The object structure coincides with our imagination! :

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

Finally, you can also write a finalize function to do the final processing of the return value of reduce:

F= (key,rval) {(key==0) {rval.msg= "a new life,baby!";} Rval}

Here the key is also the above key, that is, or age,rval is the return value of reduce, so rval an example such as: {age:0,names:["Name_6", "Name_12", "Name_18"]},

Here the key is not 0, if it is the Rval object plus msg attribute, obviously can also judge Rval.age==0, because key and rval.age are equal!!

The rest of the options here will not be said, a look will know.

Run:

Db.runcommand ({mapreduce: "T", Map:m, Reduce:r, Finalize:f, out: "T_age_names"})

Results imported into the T_age_names collection, the query is exactly what I want to see the structure of the document, it is not difficult to find that the _id is Key,value is the return value after processing.


MongoDB MapReduce Learning Notes

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.