The MapReduce programming model of "MongoDB" MongoDB database

Source: Internet
Author: User
Tags emit

When I first started reading the MongoDB starter manual, I saw MapReduce when it felt so difficult that I ignored it directly. Now re-see this part of the knowledge, the pain of the determination to learn this knowledge.

I. Concept Description

MongoDB's mapreduce is equivalent to "group by" in MySQL, and it is easy to use MapReduce to perform parallel data statistics on MONGODB; use MapReduce to implement two functions: map and reduce.

The map function calls emit (Key,value) to traverse all the records in the collection, passing key and value to the reduce function to perform the processing. The map function and the reduce function can be implemented using JavaScript. Let's learn the method parameters for the MapReduce:


Ii. examples and explanations1. Create Initialization Data


2. Map Grouping

The map function must call emit (Key,value) to return a key-value pair, using this to access the currently pending document. In this example, the Map function groups students tables by ClassID:


Value can be passed using JSON object (multiple property values are supported), as shown in the following code:

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

3. Reduce aggregation calculation

The reduce function passes a parameter similar to the group effect, combining the sequence of key values returned by the map into {key,[value1,value2,value3,...] Pass to reduce as shown in the following code:


The reduce function counts these values, in this case, the reduce function performs a summation of the number of records for the class individually, and the result is a JSON object

4. Result gets results

How the result is obtained after calculation is the function of the result function. You can execute a DB. result set. Find () can get results. Where the result set can be specified by an out variable.


5. Finialize formatted output

Use Finalize () to format the output style of the results of reduce.


6. Options Custom Output

You can also add more control details by simply adding a query parameter to the definition of the RES function to further filter the result set, as shown in the following code:


Compare Query Results:


The MapReduce programming model of "MongoDB" MongoDB database

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.