[MongoDB] MapReduce programming model of MongoDB database, mongodbmapreduce

Source: Internet
Author: User
Tags emit

[MongoDB] MapReduce programming model of MongoDB database, mongodbmapreduce

When I first read the Mongodb getting started manual, I saw mapreduce. It was so difficult that I ignored it directly. Now, when I see this part of knowledge again, I am determined to learn it.

1. Concepts

MongoDB MapReduce is equivalent to "group by" in Mysql. It is easy to use mapreduce to execute parallel data statistics on mongodb. To use MapReduce, two functions are required: map and reduce.

The map function calls emit (key, value) to traverse all the records in the collection and pass the key and value to the Reduce function for execution. Map and Reduce functions can be implemented using javascript. Next we will learn the mapreduce method parameters:


Ii. Example 1. Create initialization data


2. Map for grouping

The Map function must call emit (key, value) to return to the key-value Pair and use this to access the document to be processed. In this example, the map function groups students tables by classid:


Value can be transmitted using json object (multiple attribute values are supported), as shown in the following code:

Emit (this. classid, {count: 1 })

3. Reduce aggregate computing

The parameters passed by the Reduce function are similar to those transmitted by the group function. The key-value sequences returned by the map are combined into {key, [value1, value2, value3,...]}. pass to reduce, as shown in the following code:


The Reduce function counts these values. In this example, the reduce function calculates the sum of the number of records in the class, and the returned result is a json object.

4. Get Result

How to obtain results after calculation is the function of result. You can execute db. Result set. Find () to obtain the result. The result set can be specified through the out variable.


5. Finialize formatted output

Finalize () can be used to format the output style of reduce results.


6. options custom output

You can also add more control details. You only need to add a query parameter to the res function definition to further filter the result set, as shown in the following code:


Compare query results:


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.