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. concept description: MongoDB's MapReduce is equivalent to "groupby" in Mysql. It is easy to use mapreduce to execute parallel data statistics on mongodb.
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. concept description: MongoDB's MapReduce is equivalent to "group by" in Mysql. It is easy to use mapreduce on mongodb to execute parallel data statistics; Use M
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. <喎?http: www.2cto.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20141206/2014120609164678.png" alt = "\">
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: