COUNTING EXAMPLE VIA SHELL:
> 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 } );});};
> r = 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, { out : {inline : 1} } );
delivers:
{
"results" : [
{
"_id" : "cat",
"value" : {
"count" : 3
}
},
{
"_id" : "dog",
"value" : {
"count" : 2
}
},
{
"_id" : "mouse",
"value" : {
"count" : 1
}
}
],
"timeMillis" : 0,
"counts" : {
"input" : 4,
"emit" : 6,
"reduce" : 2,
"output" : 3
},
"ok" : 1,
}
LUAMONGO
local n = "test.things"
local m = "function(){this.tags.forEach(function(z){emit( z , { count : 1 } ) ; }) ; } ;"
local r = "function( key , values ){ var total = 0 ; for ( var i=0 ; i<values.length; i++ ) total += values[i].count ; return { count : total } ; } ;"
local q = {query = {}} --, out = {inline = 1}}
local o = "example"
local res,err = mdb:mapreduce(n, m, r, q, o)
this code creates a valid return table but doesn't process the records.
ok 1
counts table: 0x7f97e0
timeMillis 1
result example
* res,err = db:mapreduce(ns, jsmapfunc, jsreducefunc, query, output)
the output file (o) is another function parameter ( -> const char *output = luaL_optstring(L, 6, ""); )
so the out param within (q) is ignored. mongo provides some nice output options (replace/merge/reduce) and
{inline :1} which creates the result collection to ram and returning directly as results array.
don't know if the current binding is capable to use this options.
since mongo doesn't allow multhithreaded MR the bigggest advantage of MR is gone but
at this stage this MR can create very easy some usefull persistent aggregate collection serverside named by (o)
so it's a nice to have. a full featured MR could be emulate with coroutines via lua or via mongo-hadoop ->
https://github.com/mongodb/mongo-hadoop
remark: out : {inline = 1} should return RESULTS -> table and out : 'cname' should return RESULT -> string (name of coll saved to current db)
see ->
http://www.mongodb.org/display/DOCS/MapReduce -> Output options
tom