转载自:

1 > db.version();
2 1.6.3

1 mongodb-2.0.jar

01 <!--
02 #shell下完整的参数列表
03 #更高版本会多几个参数
04 //-->
05 db.runCommand( 
06
07     mapreduce : <collection>, 
08     map : <mapfunction>, 
09     reduce : <reducefunction> 
10     [, query : <query filter object>] 
11     [, sort : <sort the query.  useful   optimization>] for 
12     [, limit : <number of objects to   from collection>] return 
13     [, out : <output-collection name>] 
14     [, keeptemp: < | >] true false 
15     [, finalize : <finalizefunction>] 
16     [, scope : <object where fields go into javascript global scope >] 
17     [, verbose :  ] true 
18 });

1 //JAVA-API中可以执行mapreduce的方法
2 //该方法只允许携带4个参数
3 public MapReduceOutput mapReduce(String map, String reduce,
4                                 String outputCollection,
5                                 DBObject query)
6                                 throws MongoException
7 //以下两个方法可以使用全部参数列表
8 public MapReduceOutput mapReduce(DBObject command) throws MongoException
9 public CommandResult command(DBObject cmd) throws MongoException

 

01 <!--
02 #shell mapreduce
03 //-->
04 res = db.runCommand({
05     mapreduce:'mo_log_201208',
06     query:{Logtime:{$gte:'20120823', $lte:'20120824'}},
07     map:function() {
08         emit({Spcode:this.Spcode, Spname:this.Spname,
09                 Consignid:this.Consignid, Consname:this.Consname,
10                 Region:this.Region, Regionname:this.Regionname,
11                 Serviceid:this.Serviceid,   
12                 Servicename:this.Servicename,
13                 Srctermid:this.Srctermid}, {count:1});
14     },
15     reduce:function(key, value) {
16         var ret = {count:0};
17         ret.count++;
18         return ret;
19     },
20     out:'tmp_mo_spcode_consignid_region_serviceid_201208_1'
21 })
01 //JAVA-API mapreduce
02     Mongo db = new Mongo("host", "port");
03     DBCollection dbc = db.getCollection("mo_log_201208");
04     String mapfun = "function() {" +
05         "emit({Spcode:this.Spcode, Spname:this.Spname, " +
06                 "Consignid:this.Consignid, Consname:this.Consname, " +
07                 "Region:this.Region, Regionname:this.Regionname, " +
08                 "Serviceid:this.Serviceid, " +  
09                 "Servicename:this.Servicename, " +
10                 "Srctermid:this.Srctermid}, {count:1});" +
11                 "}";
12     String reducefun = "function(key, value) {" +
13         "var ret = {count:0};" +
14         "ret.count++;" +
15         "return ret;" +
16         "}";
17     DBObject query = new BasicDBObject("Logtime",
18                         new BasicDBObject("$gte", "20120823").append("$lte", "20120824"));
19     MapReduceOutput mro = dbc.mapReduce(mapfun, reducefun,
20                             "tmp_mo_spcode_consignid_region_serviceid_201208_1", query);
01 <!--
02 #该mapreduce比上一个多了两个参数
03 //-->
04 res = db.runCommand({
05     mapreduce:'mo_log_201208',
06     query:{Logtime:{$gte:'20120823', $lte:'20120824'}},
07     map:function() {
08         emit({Spcode:this.Spcode, Spname:this.Spname,
09         Consignid:this.Consignid, Consname:this.Consname,
10         Region:this.Region, Regionname:this.Regionname,
11         Serviceid:this.Serviceid, Servicename:this.Servicename,
12         Srctermid:this.Srctermid}, {count:1});
13     },
14     reduce:function(key, value) {
15         var ret = {count:0};
16         ret.count++;
17         return ret;
18     },
19     finalize:function(key, value){
20         db.tmp_mo_spcode_consignid_region_serviceid_201208.insert({
"_id":key, "value":value});
21         return value;
22     },
23     out:'tmp_mo_spcode_consignid_region_serviceid_201208_1',
24     verbose:true
25 })
01 //JAVA-API mapreduce
02     Mongo db = new Mongo("host", "port");
03     DBCollection dbc = db.getCollection("mo_log_201208");
04     String mapfun = "function() {" +
05         "emit({Spcode:this.Spcode, Spname:this.Spname, " +
06                 "Consignid:this.Consignid, Consname:this.Consname, " +
07                 "Region:this.Region, Regionname:this.Regionname, " +
08                 "Serviceid:this.Serviceid, " +  
09                 "Servicename:this.Servicename, " +
10                 "Srctermid:this.Srctermid}, {count:1});" +
11                 "}";
12     String reducefun = "function(key, value) {" +
13         "var ret = {count:0};" +
14         "ret.count++;" +
15         "return ret;" +
16         "}";
17     String finalizefun = "function(key, value){" +
18         "db.tmp_mo_spcode_consignid_region_serviceid_201208.insert({"_id":key, "value":value});" +
19         "return value;" +
20         "}";
21     DBObject query = new BasicDBObject("Logtime",
22                         new BasicDBObject("$gte", "20120823").append("$lte", "20120824"));
23     DBObject command = new BasicDBObject();
24     command.put("mapreduce", "mo_log_201208");
25     command.put("query", query);
26     command.put("map", mapfun);
27     command.put("reduce", reducefun);
28     command.put("finalize", finalizefun);
29     command.put("out", "tmp_mo_spcode_consignid_region_serviceid_201208_1");
30     command.put("verbose", true);
31     //在dbcollection上执行mapreduce
32     MapReduceOutput mro = dbc.mapReduce(command);
33     //在db上使用command执行mapreduce
34     CommandResult cr = db.command(command);

>>以上3种方法都可以执行mapreduce, 参数和返回结果各不相同, 根据自己的需要和使用习惯选择合适的方法

>>新版的Mongodb增加了新的聚合框架, 有更多的参数选择, 参见