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?

Derlin
  • 9,572
  • 2
  • 32
  • 53
Cengiz Poyraz
  • 11
  • 1
  • 4

1 Answers1

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.

Community
  • 1
  • 1
Derlin
  • 9,572
  • 2
  • 32
  • 53