Define a Function

You can create and configure functions in your application at any time from the Realm UI or by importing a function configuration file.


To define a new server-side function from the Realm UI:

  1. Click Functions in the left navigation menu.
  2. Click New Function in the top right of the Functions page.

Enter a unique, identifying name for the function in the Name field. This name must be distinct from all other functions in the application.


Functions in MongoDB Realm always execute in the context of a specific application user or as a system user that bypasses rules. To configure the function's execution user, specify the type of authentication that Realm should use.

Authentication Type
Application Authentication
This type of authentication configures a function to run in the context of the existing application user that was logged in when the client application called the function. If the function was called from another function then it inherits the execution user from that function.
This type of authentication configures a function to run as a system user that has full access to MongoDB CRUD and Aggregation APIs and is not affected by any rules, roles, or permissions.
User ID
This type of authentication configures a function to always run as a specific application user.
This type of authentication configures a function to run as a specific application user determined based on the result of a custom function that you define. The function must return a specific user's id string or can specify a system user by returning { "runAsSystemUser": true }.

By default, Realm includes the arguments that a function received in the log entry for each execution of the function. If you want to prevent Realm from logging the arguments, disable Log Function Arguments.


You can dynamically authorize requests based on the contents of each request by defining a Can Evaluate JSON expression. Realm evaluates the expression whenever the function is called. If you do not specify an expression then Realm automatically authorizes all authenticated incoming requests.

The expression can expand standard expression variables, including the %%request and %%user expansions.


By default, you can call a function from client applications as well as other functions in the same application. You can prevent client applications from seeing or calling a function by setting Private to true.

You can still call a private function from JSON expressions and other functions, including incoming webhooks and triggers.


Once you've created and configured the new function, it's time to write the actual javascript code that runs when you call the function. You can write the code directly in the Realm UI using the function editor.


You can use most modern (ES6+) JavaScript features in functions, including async/await, destructuring, and template literals.

From the function's Settings page:

  1. Click the Function Editor tab.
  2. Add javascript code to the function. At minimum, the code must assign a function to the global variable exports, as in the following example:

    exports = function() {
    return "Hello, world!";

Once you've written the function code, click Save from either the Function Editor or Settings tab.

After you save the function, you can begin using it immediately. For more information on using the function, see Call a Function.

Give Feedback