Map/Reduce Using Stored Javascript By Java Driver

171 views
Skip to first unread message

玉 付

unread,
Mar 24, 2011, 7:26:41 AM3/24/11
to mongodb-user
MongoDB v1.6.2

Given stored map and reduce and finalize functions as follows:
> db.system.js.find({},{_id:1})
{ "_id" : "mm" }
{ "_id" : "rr" }
{ "_id" : "ff" }

But the following command is wrong , because map function will emit
sth
> db.f.mapReduce("mm","rr")
{
"result" : "tmp.mr.mapreduce_1300965424_25",
"timeMillis" : 2,
"counts" : {
"input" : 2,
"emit" : 0,
"output" : 0
},
"ok" : 1,
}

So I change the command, and the result is correct.
> db.f.mapReduce(db.eval("mm"),db.eval("rr"))
{
"result" : "tmp.mr.mapreduce_1300965446_26",
"timeMillis" : 4,
"counts" : {
"input" : 2,
"emit" : 4,
"output" : 1
},
"ok" : 1,
}

Then I write it in java
MapReduceOutput mrout = coll.mapReduce("db.eval('mm')",
"db.eval('rr')", "mrout", null);

Here is the exception:
com.mongodb.CommandResult$CommandFailure: command failed [command
failed [mapreduce] { "assertion" : "map invoke failed: JS Error:
uncaught exception: {\n\t\"assertion\" : \"internal error: locks are
not upgradeable: { \\\"opid\\\" : 85231711, \\\"active\\\" : true, \\
\"waitingForLock\\\" : false, \\\"secs_running\\\" : 0, \\\"op\\\" : \\
\"query\\\", \\\"ns\\\" : \\\"?\\\", \\\"query\\\" : { \\\"$eval\\\" :
\\\"mm\\\" }, \\\"client\\\" : \\\"0.0.0.0:0\\\", \\\"desc\\\" : \\
\"conn\\\" }\",\n\t\"assertionCode\" : 10293,\n\t\"errmsg\" : \"db
assertion failure\",\n\t\"ok\" : 0\n}" , "assertionCode" : 9014 ,
"errmsg" : "db assertion failure" , "ok" : 0.0}

Does it mean we cannot call mapReduce using stored js function?

Nat

unread,
Mar 24, 2011, 8:04:28 AM3/24/11
to mongodb-user
can you list the actual result of db.system.js.find({},{_id:1})
including the script?

Scott Hernandez

unread,
Mar 24, 2011, 8:20:16 AM3/24/11
to mongod...@googlegroups.com
For this you should not use db.eval with map/reduce params. Just us
the function names stored in system.js

MapReduceOutput mrout = coll.mapReduce("mm", "rr", "mrout", null);

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Nat

unread,
Mar 24, 2011, 8:26:31 AM3/24/11
to mongodb-user
I think to use it from java, you need to retrieve the map/reduce
script from db.system.js using find() method manually. From my test,
it looks like 1.6.x doesn't seem to support map reduce stored js
functions.

axlfu

unread,
Mar 24, 2011, 9:36:22 AM3/24/11
to mongodb-user
@Nat
> db.system.js.find({},{_id:1})
{ "_id" : "mm" }
{ "_id" : "rr" }
{ "_id" : "ff" }
>
You mean we cannot write java code to implement this requirement?
Will mongoDB v1.8.0 and Java Driver 2.5.1 support ?

Eliot Horowitz

unread,
Mar 24, 2011, 4:35:43 PM3/24/11
to mongod...@googlegroups.com
Its working for a different reason than you think.
db.eval is actually just pulling the source code into shell.

you want
db.f.mapReduce("mm( this )","rr( this )")

or something like that


On Thu, Mar 24, 2011 at 7:26 AM, 玉 付 <freese...@gmail.com> wrote:

axlfu

unread,
Mar 24, 2011, 10:10:18 PM3/24/11
to mongodb-user
Well , it's map reduce, of course can not pass this to map and reduce
function
And result is also an exception:

com.mongodb.CommandResult$CommandFailure: command failed [command
failed [mapreduce] { "assertion" : "map invoke failed: JS Error:
TypeError: this.scheme has no properties nofile_b:1" ,
"assertionCode" : 9014 , "errmsg" : "db assertion failure" , "ok" :
0.0}

Here is my map function segment:
mm = function (){
this.scheme.forEach(function (z) {if (z.status==0)
...
...

Eliot Horowitz

unread,
Mar 24, 2011, 11:07:19 PM3/24/11
to mongod...@googlegroups.com
Ah, you need to either not use this and pass a paramter mm = function(
obj ) { ... }
or do mm.apply( this )

2011/3/24 axlfu <freese...@gmail.com>:

axlfu

unread,
Mar 25, 2011, 2:45:32 AM3/25/11
to mongodb-user
Would u plz give me a complete example?

Honestly, I am totally confused......

Not only map function ,but also reduce and finalize function, how to
do so with java driver 2.5.1 and mongodb 1.8.0?

Thanks very much

On 3月25日, 上午11时07分, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> Ah, you need to either not use this and pass a paramter mm = function(
> obj ) { ... }
> or do mm.apply( this )
>
> 2011/3/24 axlfu <freesearc...@gmail.com>:

Nat

unread,
Mar 25, 2011, 3:09:29 AM3/25/11
to mongodb-user
It seems to be too complicated for my liking... but if your M/R
functions are m, r respectively:

> db.foo.mapReduce("function() { m.apply(this); }","function(k,vals) { return r(k, vals); }")

axlfu

unread,
Mar 25, 2011, 6:47:36 AM3/25/11
to mongodb-user
Thanks very much for your patience
Reply all
Reply to author
Forward
0 new messages