MongoDB也简单的实现了MapReduce的功能来提供分布式的数据查询服务,MapReduce的分布是功能主要用在Shard上
db.runCommand(
{ mapreduce : <collection>,
map : <mapfunction>,
reduce : <reducefunction>
[, query : <query filter object>]
[, sort : <sort the query. useful for optimization>]
[, limit : <number of objects to return from collection>]
[, out : <output-collection name>]
[, keeptemp: <true|false>]
[, finalize : <finalizefunction>]
[, scope : <object where fields go into javascript global scope >]
[, verbose : true]
}
);
{ mapreduce : <collection>,
map : <mapfunction>,
reduce : <reducefunction>
[, query : <query filter object>]
[, sort : <sort the query. useful for optimization>]
[, limit : <number of objects to return from collection>]
[, out : <output-collection name>]
[, keeptemp: <true|false>]
[, finalize : <finalizefunction>]
[, scope : <object where fields go into javascript global scope >]
[, verbose : true]
}
);
下面是对MapReduce的简单测试
此例子来源于:http://www.mongodb.org/display/DOCS/MapReduce
> db.things.insert({_id:1,tags:['dog','cat']});
> db.things.insert({_id:2,tags:['cat']});
> db.things.insert({_id:3,tags:['mouse','cat','dog']});
> db.things.insert({_id:4,tags:[]});
> m = function(){
... this.tags.forEach(
... function(z){
... emit(z,{count:1});
... }
... );
};
function () {
this.tags.forEach(function (z) {emit(z, {count:1});});
}
> r=function(key,values){
... var total = 0;
... for(var i=0;i<values.length;i++)
... total += values[i].count;
... return {count:total};
... };
function (key, values) {
var total = 0;
for (var i = 0; i < values.length; i++) {
total += values[i].count;
}
return {count:total};
}
> res=db.things.mapReduce(m,r);
{
"result" : "tmp.mr.mapreduce_1268577545_1",
"timeMillis" : 25,
"counts" : {
"input" : 4,
"emit" : 6,
"output" : 3
},
"ok" : 1,
"ok" : 1,
}
> res
{
"result" : "tmp.mr.mapreduce_1268577545_1",
"timeMillis" : 25,
"counts" : {
"input" : 4,
"emit" : 6,
"output" : 3
},
"ok" : 1,
"ok" : 1,
}
> db[res.result].find()
{ "_id" : "cat", "value" : { "count" : 3 } }
{ "_id" : "dog", "value" : { "count" : 2 } }
{ "_id" : "mouse", "value" : { "count" : 1 } }
> db[res.result].drop()
true
> db[res.result].find()
>
> db.things.insert({_id:2,tags:['cat']});
> db.things.insert({_id:3,tags:['mouse','cat','dog']});
> db.things.insert({_id:4,tags:[]});
> m = function(){
... this.tags.forEach(
... function(z){
... emit(z,{count:1});
... }
... );
};
function () {
this.tags.forEach(function (z) {emit(z, {count:1});});
}
> r=function(key,values){
... var total = 0;
... for(var i=0;i<values.length;i++)
... total += values[i].count;
... return {count:total};
... };
function (key, values) {
var total = 0;
for (var i = 0; i < values.length; i++) {
total += values[i].count;
}
return {count:total};
}
> res=db.things.mapReduce(m,r);
{
"result" : "tmp.mr.mapreduce_1268577545_1",
"timeMillis" : 25,
"counts" : {
"input" : 4,
"emit" : 6,
"output" : 3
},
"ok" : 1,
"ok" : 1,
}
> res
{
"result" : "tmp.mr.mapreduce_1268577545_1",
"timeMillis" : 25,
"counts" : {
"input" : 4,
"emit" : 6,
"output" : 3
},
"ok" : 1,
"ok" : 1,
}
> db[res.result].find()
{ "_id" : "cat", "value" : { "count" : 3 } }
{ "_id" : "dog", "value" : { "count" : 2 } }
{ "_id" : "mouse", "value" : { "count" : 1 } }
> db[res.result].drop()
true
> db[res.result].find()
>
以下有几个MapReduce的参考例子:
http://www.mongodb.org/display/DOCS/MapReduce
http://github.com/mongodb/mongo/ ... sts/mr_bigobject.js
http://github.com/mongodb/mongo/blob/master/jstests/mr5.js
http://github.com/mongodb/mongo/blob/master/jstests/mr4.js
http://github.com/mongodb/mongo/blob/master/jstests/mr3.js
http://github.com/mongodb/mongo/blob/master/jstests/mr2.js
http://github.com/mongodb/mongo/blob/master/jstests/mr1.js