0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I think that function should be able to be used with queries. Could you give an example with aggregate function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.