Executing Stored Function with params

468 views
Skip to first unread message

M.

unread,
Nov 18, 2011, 8:13:38 PM11/18/11
to node-mongodb-native
Hello,

I'm trying to execute a stored function like this from node.js.

this.db.eval('identity', 100, function(err, doc) {
console.log(doc);
});

Inside the database, there is identity function like this.

db.system.js.save(
{
_id : 'identity',
value : function (id) {
return id;
}
}
);

The result I want is 100 but it returns identity function itself. It
could be done something like db.eval('identity(100)', function(..)
{...})
However, I'd really love to pass parameters by object format, not by
textual representation.

Any idea?

Thanks.

christkv

unread,
Dec 17, 2011, 5:17:37 PM12/17/11
to node-mong...@googlegroups.com
examples of usage in


but basically

client.eval('function (x, y) {return x + y;}', [2, 3], function(err, result) {
  test.equal(5, result);
});

Phill Rosen

unread,
Sep 14, 2012, 3:56:28 PM9/14/12
to node-mong...@googlegroups.com
Did you ever get this resolved? I am seeing issues with this currently. Save database, same credentials.

From console I do:
db.system.js.save({ "_id" : "echo", "value" : function(x){return x;} })  
> db.eval("echo('test')")
test

However from the node driver:
db.eval("echo('test')", function (err, res) {
console.log(err);
}

Produces:
eval failed: invoke failed: JS Error: ReferenceError: echo is not defined nofile... (length: 84)

christkv

unread,
Sep 16, 2012, 11:07:28 PM9/16/12
to node-mong...@googlegroups.com
can you post a ticket with some code on github. I'll have a look during the week.

One big observation that will save you a lot of pain and suffering. Don't execute javascript on the server as part of you business logic it will seriously hamper your scalability. The reasons are very simple.

1. The javascript engine is single threaded (spidermonkey)
2. Eval will take out a write lock for the entire duration of the execution limiting your ability to scale writes.

I know it's tempting but javascript on mongodb was never designed to be mongodb stored procedures. The only valid reason is if you are using them during f.ex map-reduce jobs.
Reply all
Reply to author
Forward
0 new messages