Map/reduce in MongoDB makes some compound queries, because MongoDB does not support group by queries, and MapReduce is similar to SQL's group by, so it can be thought that MapReduce is the MongoDB version of group B Y
The command is as follows:
Db.runcommand ({mapreduce:, map:, reduce: [, query:] [ , sort:] [, limit:] [, Out:] [, Keeptemp:] [, Finalize:] [, SC
Ope:] [, Verbose:true]}); MapReduce: Specifies the collection Map:map function Reduce:reduce function to be processed by MapReduce query: A filter condition that only satisfies the condition
Row is added to the MapReduce collection, which is the sort: and query-combined sort parameter that is executed before the entire mapreduce process, which is the only place to optimize the grouping mechanism: ibid. Out: The collection name of the output of the result, does not specify that a random name will be created by default collection Keytemp:true or FALSE, indicating whether the output to the collection is temporary, if the TR UE, it will be automatically deleted after the client connection is interrupted, if you use 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: Like Map,reduce, it is a function that calculates a result of reduce and then evaluates the key and value once and returns a final result.
Scope: Sets the value of the parameter, where the value set here is visible in the Map,reduce,finalize function verbose: Prints debug information during execution.
The return result structure is as follows:
{result:, Counts: {input:, emit:, Output: }, Timemillis:, OK: <1_if_ok>, [, Err:]} result: The Colle that stores the result ction Name Input: The number of data rows that satisfy the condition emit:emit the number of calls, that is, the total amount of data in all collections ouput: Returns the number of result bars time Millis: Execution time, Ms OK: successful, 1 err: If it fails, there can be a reason for failure, but from experience, the reason is rather vague, the effect is small
Map function
m= function () {
emit (This.hs,
{
"customid_data": [{Customid:this.customid}],
"Salescountry_data": [{salescountry:this.salescountry}],
"Usd_data": THIS.USD,
"Num_data": This.num,
"Compid_data": [{ Compid:this.compid}]
}
)
}
Reduce function:
R=function (key,values) {var ret = {"Customid_data": [], "Salescoun
Try_data ": []," Usd_data ": 0," Num_data ": 0," Compid_data ": []
};
var customid= {};
var salescountry={};
var compid={};
for (Var i=0;i<values.length;i++) {var ia = values[i]; for (Var j in Ia.customid_data) {if (!customid[ia.customid_data[j].customid]) {C
Ustomid[ia.customid_data[j].customid]=true;
Ret.customid_data.push (Ia.customid_data[j]); }} for (Var j in Ia.salescountry_data) {if (!salescountry[ia.salescount
Ry_data[j].salescountry]) {salescountry[ia.salescountry_data[j].salescountry]=true; Ret.salescountry_data.push (iA.SALESCOUNTRY_DATA[J]); }} for (Var j in Ia.compid_data) {if (!compid[ia.compid_data[j].compid]
) {compid[ia.compid_data[j].compid]=true;
Ret.compid_data.push (Ia.compid_data[j]);
}} Ret.usd_data+=values[i].usd_data;
Ret.num_data+=values[i].num_data;
} return ret; }
Finalize function:
F=function (key, values) {
return
{
custom:values.customid_data.length,
salescountry: Values.salescountry_data.length,
usd_sum:values.usd_data,
num_sum:values.num_data,
comp: Values.compid_data.length
};
}
Execute the MapReduce function
Db.collect.mapReduce (m,r,{out: ' Groupbyhs ', Query:{isimp: ' 1 '},keytemp:false,finalize:f})
If you want to use features such as sort and limit, you can use Res.find (). Sort ({"Value.totalnum": -1}). Limit (2) to achieve the purpose
It is important to note that the return value of the reduce function also needs to be consistent with the second parameter structure of the reduce function.
Reference article:
Some practical experiences on using MapReduce in MongoDB
http://www.l99.com/EditText_view.action?textId=461413
If you want to use features such as sort and limit , you can use Res.find (). Sort ({"Value.totalnum": -1}). Limit (2) to achieve the purpose (say good)
implements MapReduce with MongoDB
/HTTP/ www.open-open.com/lib/view/open1329107420952.html
Mongodb mapreduce programming Model
/HTTP chenzhou123520.iteye.com/blog/1637672
MongoDB MapReduce (EXT)
http://javabeezer.iteye.com/blog/ 1275124
So the return value of the reduce function also needs to be consistent with the second parameter structure of the reduce function (this is a very good saying.)
[Turn]mongodb MapReduce usage and introduction
http://blog.163.com/sjy_814/blog/static/77801164201282531137196/
MongoDB Group, go to *******************************
http://my.oschina.net/huzorro/blog/73879