Docs Menu

Call a Function - Node.js SDKicons/link.png

On this page

  • Call a Function by Name

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

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

To call a function, you can either pass its name and arguments to User.callFunction() or call the function as if it was a method on the User.functions property.

Link a MongoDB Atlas Data Source

This example requires a MongoDB Realm Application with a linked Atlas data source. Replace the "<appId>" in the code block with your app id, available in the left navigation menu of the MongoDB Realm UI.

// wrap the code below in an async function to 'await' for the promises to resolve
const numA = 2;
const numB = 3;
const myArgs = [numA, numB];
const result = await user.functions.sum(numA, numB);
const resultOfCallFunction = await user.callFunction("sum", myArgs); // alternate syntax to call a MongoDB Realm Function
`Using the "functions.sum()" method: the sum of ${numA} + ${numB} = ${result}`
`Using the "callFunction()" method: the sum of ${numA} + ${numB} = ${resultOfCallFunction}`

When you run the code sample, your output should resemble the following:

Using the "functions.sum()" method: the sum of 2 + 3 = 5
Using the "callFunction()" method: the sum of 2 + 3 = 5
Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.


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