1, overview
MapReduce is a very flexible and powerful data aggregation tool. The advantage of this is that a single aggregation task can be decomposed into several small tasks that are assigned to parallel processing on multiple servers. MongoDB also provides mapreduce, of course the query language must be JavaScript.
The MapReduce in MongoDB corresponds to group by in the relational database. Two function map and reduce functions are implemented using MapReduce. 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.
2, basic syntax
Db.runcommand ({mapreduce:<collection>, Map:<mapfunction>, reduce:<reducefunction>, [, Query:<query Filter Object>][,sort:<sorts the input objects using ThisKey. Useful forOptimization,like sorting by the emit 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]});
Parameter description:
Mapreduce: The set of targets to manipulate
Map: Map function (generate key-value pair sequence as the reduce function parameter)
Reduce: Statistical functions
Query: Target Record filtering
Sort: Target record sorting
Limit: Limits the number of target records
Out: The statistics result holds the collection (does not specify the use of temporary collection, after the client disconnects automatically delete)
Keeptemp: Whether to keep temporary collections
Finalize: 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.
3, Application examples
(1) Query the active table, the number of times each CID corresponds. Equivalent to grouping in CID.
map=function() {Emit ( This. cid,{count:1})}reduce=function(key,values) {varCnt=0; Values.foreach (function(Val) {cnt+=Val.count;}); return{"Count": cnt};} Db.active.mapReduce (map,reduce,{out:' MR1 '}) (2) group map by CID and date=function() {emit ({CDI: This. CID,CD: This. cd},{count:1})}reduce=function(key,values) {varCnt=0; Values.foreach (function(Val) {cnt+=Val.count;}); return{"Count": cnt};} Db.active.mapReduce (map,reduce,{out:' MR2 '}) (3) The number of products per CID, the total amount of the map=function() {Emit ( This. Cid,{amount: This. price,count:1})}reduce=function(key,values) {varres={amount:0,count:0} values.foreach (function(val) {Res.amount+=Val.amount; Res.count+=Val.count}); returnRes;} Db.test.mapReduce (map,reduce,{out:"MR3"})
My summary: In MongoDB, map groups the collection. Reduce summarizes the grouped results.
MongoDB learns 3---MONGO's mapreduce