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: String, collection name,

Map: function, see below

Reduce: function, see below

[, Query: Document, send the transition document before sending to the map function]

[, Sort: Document, sort documents before sending to the map function]

[, Limit: integer, maximum number of documents destined for map function]

[, Out: String, collection of statistical results saved]

[, Keeptemp: Boolean value, whether the temporary result collection is saved when the link is closed]

[, Finalize: function, give the result of reduce to this function, do the final processing]

[, Scope: Document, variable to be used in JS code]

[, Jsmode: Boolean value, whether to reduce the conversion of BSON and JS during execution, default true] //Note: false when bson-->js-->map-->bson-->js--> Reduce-->bson, can handle very large mapreduce,<br>//true when bson-->js-->map-->reduce-- >bson

[, Verbose: Boolean value, whether to produce a more verbose server log, default 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 ~

var m=function() {    emit (this. Age,this. 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:

First group {key:0,values: ["Name_6", "Name_12", "name_18"] 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:

var r=function(key,values) {    var ret={age:key,names:values};     return 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:

var f=function(key,rval) {    if(key==0) {        rval.msg= "A new life, baby! " ;    }     return 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.