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).
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:
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.
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.
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.
There are a number of reasons why you may want to transform your response using a Transformer. For example, you may want to:
Let’s take a look at an example below:
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:
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:
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.
Add any sortable fields here. Once specified, the sortable field will be available in components like table and card list.
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.
You can configure permissions for MongoDB Functions in Roles & Permissions within Company Settings. Learn more.