MongoDB's Map reduce

Source: Internet
Author: User

MongoDB's map reduce uses

Example:

res = Db.runcommand ({
MapReduce: ' Liveepguservisits ',
Map:function () {
Emit ({Provice:this.provice}, {"Data": [{"Mac": This.mac}],visit:this.visitnum, usercount:0});
},
Reduce:function (key, value) {
var ret = {data:[]}, visit=0
var usercount=0;
var macs = {};
var sum = 0;
for (var i in value) {
var ia = value[i];
for (Var j in Ia.data) {
if (!macs[ia.data[j].mac]) {
Macs[ia.data[j].mac] = true;
Ret.data.push (Ia.data[j]);

UserCount + = 1;

}
}

Sum + = number (ia.visit);
}
Ret.visit = sum;
Ret.usercount = UserCount;
return ret;
},
query:{"Inputtime": {$gte: Isodate ("2014-09-17t14:20:00z"), $lte: Isodate ("2014-09-17t14:30:00z")}},
Finalize:function (key, values) {
return [{count:values.data.length},{visit:values.visit},{usercount:values.usercount}];
},
Out: ' Tmp_mo_spcode_consignid_1 ',
Verbose:true
})The above example,Map:key is the province where value is the corresponding Mac value, number of visits, and number of users in the province. reduce: Gets the value to each province, and then the operation is performed, returning the result. specifies the output format through the Finalize parameter. If you do not specify an output format, the map's key and value formats are installed for output.

< Span style= "Font-family:verdana; Background-color:inherit "> where Db.runcommand contains the following parameters:

db.runcommand ({mapreduce: <collection>, map: <mapfunction>, reduce: <red  ucefunction> [, Query: <query filter object>] [, Sort: <sort the query. Useful for Optimization>] [, Limit: <number of objects to return from Collection>] [, Out: <output-colle  Ction Name>] [, Keeptemp: <true|false>] [, Finalize: <finalizefunction>] [, Scope: <object where Fields go to JavaScript global scope;] [, Verbose:true]});   

 -MapReduce: Specifies the collection to be processed for MapReduce
-Map:map function-Reduce:reduce function-out: The name of the collection that outputs the result, does not specify a collection that will create a random name by default (if the Out option is used, you do not have to specify keeptemp:true because it is already hidden)-Query: A filter condition in which only documents that meet the criteria call the map function. (Query. Limit,sort can be combined freely)-Sort: Sort parameters combined with limit (also sort documents before they are sent to the map function) to optimize the grouping mechanism-Limit: The maximum number of documents to be sent to the map function (if no limit is used, using sort alone is not very useful)-Keytemp:true or FALSE, indicating whether the output to the collection is temporary, if you want to keep the collection after the connection is closed, specify keeptemp to True if you are using a MongoDB MONGO client connection, It must exit before it is deleted. If the script is executed, the script exits or calls close to automatically delete the result collection-Finalize: is a function that performs a calculation of key and value once and returns a final result after executing map, reduce, which is the last step in the process, so finalize is the right time to calculate the average, trim the array, and clear the excess information -variables to be used in the Scope:javascript code, variables defined here are visible in the Map,reduce,finalize function-verbose: Verbose output option for debugging, you can set it to true if you want to see the mpareduce running process. You can also print out the information in the map,reduce,finalize process to the server log.

MongoDB's Map reduce

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.