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?
Asked
Active
Viewed 527 times
0

Derlin
- 9,572
- 2
- 32
- 53

Cengiz Poyraz
- 11
- 1
- 4
1 Answers
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.
-
I think that function should be able to be used with queries. Could you give an example with aggregate function? – Cengiz Poyraz Apr 17 '17 at 09:22