Cengiz Poyraz
Cengiz Poyraz

Reputation: 11

How to call custom server side function with mongodb nodejs driver

I add a function to system.js that returns if number is odd or not and could not call it with aggregate function of nodejs driver. How can I call it?

Upvotes: 0

Views: 540

Answers (1)

Derlin
Derlin

Reputation: 9881

To call a custom function:

You can use db.eval(). For example:

db.eval("echo(5)", function(err, result) {
    assert.equal(null, err);
    assert.equal(5, result);
});

But note that, as the documentation suggests, defining and calling a system level javascript function NOT recommended.

To use a function in an aggregation:

Basically, external/custom functions don't work with the aggregation framework. Everything is parsed to BSON on input, so no JavaScript or anything else is allowed.

Have a look at Call function inside mongodb's aggregate? to find a workaround.

Upvotes: 1

Related Questions