MongoDB Finishing Note のmapreduce

Source: Internet
Author: User
Tags emit

Mongdb's mapreduce is equivalent to "group by" in MySQL, so it's easy to use map/reduce for parallel "stats" on MongoDB.

Using MapReduce to implement the two function map functions and the reduce function, the map function calls emit (Key,value), iterates through all the records in the collection, and passes the key and value to the reduce function for processing. The map function and the reduce function can be implemented using JS, and a mapreduce operation can be performed with the Db.runcommand or MapReduce command.

Sample Shell

Db.runcommand ({mapreduce:<collection>, Map:<mapfunction>, reduce:<reducefunction>[, Query:<query Filter Object>[, Sort:<sorts the input objects using ThisKey. Useful foroptimization, like sorting by Theemit key forFewer reduces>[, Limit:<number of objects toreturnFrom collection>[, Out:<see Output Options Below>[, Keeptemp:<true|false>[, Finalize:<finalizefunction>[, Scope:<object where fields go into JavaScript global scope >[, Verbose:true]});
View Code

Parameter description:
MapReduce: The set of targets to manipulate.
Map: Map functions (Generate key-value pairs of sequences, as parameters of the reduce function).
Reduce: Statistical functions.
Query: Target record filtering.
Sort: Target record sorting.
Limit: Limits the number of target records.
Out: The statistical result holds the collection (without specifying the use of a temporary collection, which is automatically deleted after the client disconnects).
Keeptemp: Whether to keep the temporary collection.
Finalize: The final processing function (save the result set after the final collation of the reduce return result).
Scope: Import external variables to map, reduce, finalize.
Verbose: Displays detailed time statistics.

Let's prepare the data for the following example

> Db.students.insert ({classid:1, age:14, Name: ' Tom '})> Db.students.insert ({classid:1, age:12, Name: ' Jacky '})> Db.students.insert ({classid:2, age:16, Name: ' Lily '})> Db.students.insert ({ Classid:2, Age:9, Name: ' Tony '})> Db.students.insert ({classid:2, age:19, Name: ' Harry '})> Db.students.insert ({classid:2, age:13, Name: ' Vincent '})> Db.students.insert ({classid:1, age:14, Name: ' Bill '})> Db.students.insert ({classid:2, age:17, Name: ' Bruce '})>
View Code

Now we show how to count the number of students in Class 1 and Class 2.

The MAP function must call emit (key, value) to return a key-value pair, using this to access the currently pending Document.

Here this must not forget!!!

> m = function () {Emit (this. ClassID, 1)}function () {Emit (this. ClassID, 1);} >

Value can be passed using JSON Object (multiple property values are supported). For example:
Emit (This.classid, {count:1})
The reduce function receives a parameter similar to the group effect, combining the sequence of key values returned by the Map into {key, [Value1,value2, Value3, value ...] and passing it to reduce.

> r == 0+=return=0 + = v;}); return x;} >

The Reduce function "counts" These values and returns the result using JSON Object.

The results are as follows:

> res = db.runcommand ({... mapreduce:"Students",... map:m,... reduce:r,... out:"Students_res" ... }); {"result": "Students_res","Timemillis": 1587,"counts": {"input": 8,"emit"  : 8,"Output": 2},"OK": 1}>"_id": 1, "value": 3"_id": 2, "value": 5 }>
View Code

MapReduce () stores the results in the "students_res" table.

Using Finalize () we can further process the results of reduce ().

return {classid:key, count:value};} function (key, value) {return  {classid:key, count:value};} >

Let's recalculate it again to see the result of the return:

> res = db.runcommand ({... mapreduce:"Students",... map:m,... reduce:r,... out:"Students_res" ,... finalize:f ...}); {"result": "Students_res","Timemillis": 804,"counts": {"input": 8,"emit": 8,"Output": 2},"OK": 1}>"_id": 1, "value": {"ClassID": 1, "Count": 3 /c11> "_id": 2, "value": {"ClassID": 2, "Count": 5 }}>
View Code

Column names are changed to "ClassID" and "count", and such lists are easier to understand.

We can also add more control details.

> res = db.runcommand ({... mapreduce:"Students",... map:m,... reduce:r,... out:"Students_res" ,... finalize:f,... query:{age:{$lt:Ten}}); {"result": "Students_res","Timemillis": 358,"counts": {"input": 1,"emit": 1,"Output": 1},"OK": 1}>"_id": 2, "value": {"ClassID": 2, "Count": 1
   
    }}>
   
View Code

You can see the first filter, only take age<10 data, and then the statistics, so there is no 1 classes of statistical data.

MongoDB Finishing Note の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.