MapReduce is grouped according to the first parameter of the emit function called in the map function.
Map-reduce is a computational model that simply executes a large amount of work (data) decomposition (MAP) and then merges the results into the final result (REDUCE).
Using MapReduce to implement the two function map functions and the reduce function, the map function calls emit (key, value), traverses all the records in the collection, and passes the key and value to the reduce function for processing. The MAP function must call emit (key, value) to return the key value pair.
Parameter description:
1. Map: Mapping function (generating a sequence of key-value pairs as the reduce function parameter).
2. Reduce statistical function, the task of the reduce function is to turn key-values into Key-value, which is to change the array of values into a single value.
3. Out statistic result holds collection (uses temporary collection without specifying, automatically deletes after client disconnects).
4. Query a filter condition that only documents that meet the criteria will call the map function. (Query. Limit,sort can be combined freely)
5. Sort and limit the sort ordering parameters (also sorting documents before sending to the map function) to optimize the grouping mechanism
6. Limit of the number of documents sent to the map function (if there is no limit, the use of sort alone is not very useful)
Test data Preparation db.user.drop ();
for (var i=10 i< i++) {Db.user.insert ({name: "user" + I, Age:Math.floor (Math.random () *10) + 20, Sex:Math.floor (Math.random () *3)%2 ==0? ' M ': ' F ', Chinese:Math.floor (Math.random () *50) +50, Math:Math.floor (Math.random () *50) +50, ENGLISH:MATH.F
Loor (Math.random () *50) +50, class: "C" + i%5})}//RunCommand run mode Db.sales.runCommand ({mapreduce: "User",
Map:function () {if (This.class = = "C1") {Emit (This.age, this.age);
}, Reduce:function (key,values) {var maxValue = Max (key, values);
return maxValue; }, {out: {inline:1}, query: ", Sort:", Limit: "",}}) Db.user.mapReduce (///mapping function, which will call emit
(Key,value), the collection is grouped according to the key you specify.
function () {//The value of the group//second parameter is passed to reduce emit (this.age, this) according to the first argument of the emit function; },//simplified functions, which group the data after the map grouping simplified//The key in reduce (Key,value) is the key in emit, Vlaues is the set function of emit (value) after grouping emit (key, Val UES) {VAR maxValue = Math.max (key, values);
return maxValue;
},//optional parameter {query: {sex: ' F '}, out: ' Result ', sort: {}, limit:0})
Execution results:
{
"result": "Result",//stored set name
"Timemillis": "
Counts": {
"input": 29,//number of incoming documents
"emit": 29, //The number of times this function has been called
"reduce": 6,//The number of times this function is called
"output": 8 //Last return the number of documents
},
"OK": 1
}
To view the results returned: