Functions
/
MongoDB Functions

MongoDB Functions

A MongoDB Function can be used to read, insert, update, or delete data from your MongoDB database. When you connect your database to Internal, basic functions corresponding to each collection in your database are automatically generated for you. You can create additional custom functions to read data from your MongoDB using the Function Editor. (Support for custom insert, update, and delete functions are coming this year).

Getting Started with the Function Editor

To create or edit a Function, you’ll need to use the Function Editor. You can access the Function Editor from one of two places:

  1. From a Space: Go into Edit Mode and click on the App Data icon in the left nav.
  2. From Company Settings: Go to Data & Functions, click on the desired database, and click on the Functions tab.

Once you open the Function Editor, name your Function and select the database (if not selected already) for which you want to create a function.

Configure

Select a collection and enter your aggregate pipeline (this must be done in strict JSON format). You can optionally include a reserved filter parameter within your aggregate pipeline to set a variable input for your function.

Example: A MongoDB function to list all comments in the database where the name equals the variable filter “name”.

For more information on aggregation, you can refer to the MongoDB documentation for quick reference and SQL-to-aggregation mapping chart.

Run Function

If you want to run a function to confirm it is working as intended, select an environment and click the button to "Run Function". 

Note: These functions run against your database. If you are creating an Insert or Update function, be extra careful as running your function will modify data in your database. We recommend setting up a staging environment and running your function against it.

Transform

There are a number of reasons why you may want to transform your response using a Transformer. For example, you may want to:

  • Transform the response using Javascript to return a specific set of fields. In many cases, your request may return too many objects, objects that are deeply nested, or otherwise in a format that doesn’t work for the tools you want to build. 
  • Transform the response using Javascript to return fields with manipulated values (e.g. split or join fields values).

Let’s take a look at an example below:

Example:

Below is the response we’re starting with. It currently returns the fields _id, date, email, movie_id, name, and text.

To return an additional field “domain” from the email field, we wrote this simple Transformer:

And here’s the new Transformed Response:

Inputs

This section is where you can view and configure function inputs. Think of inputs as fields in a form component, a primary key input that is used to retrieve a record, or filter or sorting inputs for a table component. There are two types if inputs supported for MongoDB Functions:

Filters

Add any filterable fields, specify the field type, and the allowed operation types. In addition, if you added a reserved filter parameter (e.g. filter.foo) to your MongoDB aggregate pipeline, you’ll also need to specify the corresponding filterable field (e.g. Foo) as an input to your function. Once specified, the filters are available in components like table, detail, and card list. Learn more about reserved parameters.

Sorting

Add any sortable fields here. Once specified, the sortable field will be available in components like table and card list. 

Outputs

This section is where you configure the outputs of your Function. You can manually add outputs or click the Update button to infer outputs from the last execution of your function. Think of Outputs as table columns or fields displayed in a detail component.

  • Name: This is the name of the attribute (e.g. column name in a table or field name in a Detail Component)
  • Type: This is where you can specify the field time that should be returned.
  • Key: This is the combination of attributes that make the row unique. Key(s) are used by additional features like automatic row updates and auto linking.

 

Permissions

You can configure permissions for MongoDB Functions in Roles & Permissions within Company Settings. Learn more.