MongoDB learns 3---MONGO's mapreduce

Source: Internet
Author: User
Tags emit

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

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.