Docs Menu

Call a Function

On this page

  • Overview
  • Call from A Function, Trigger, or HTTP Endpoint
  • Call from a JSON Expression
  • Call from Realm CLI
  • User Functions
  • System Functions
  • Call from a Client Application

You can call a function from other parts of your Realm app, from a connected client application, or with Realm CLI.

The examples in this section demonstrate calling a simple function named sum that takes two arguments, adds them, and returns the result:

// sum: adds two numbers
exports = function(a, b) {
return a + b;
};

You can call a function from another function, including HTTPS endpoints and triggers, by accessing function context with the context.functions global variable:

For additional information, see the Function Context reference page.

// difference: subtracts b from a using the sum function
exports = function(a, b) {
return context.functions.execute("sum", a, -1 * b);
};

You can call a function from a rule expression, including service rules, by using the %function operator:

{
"numGamesPlayed": {
"%function": {
"name": "sum",
"arguments": [
"%%root.numWins",
"%%root.numLosses"
]
}
}
}

To call a function with Realm CLI, run realm-cli function run and specify the function name and any arguments. The command returns the function result as EJSON as well as any log or error messages.

To call a function in the context of a specific user, include their User ID in the --user argument.

realm-cli function run \
--function=sum \
--user=61a50d82532cbd0de95c7c89 \
--args=1 --args=2

To call a function in the system context, do not include the --user argument. System functions bypass all rules and do not enforce or validate schemas.

realm-cli function run \
--function=sum \
--args=1 --args=2

You can call a function from client applications that are connected with a Client SDK or over the wire protocol.

Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.