Navigation

Triggers

Triggers allow you to execute server-side logic in response to database events or according to a schedule. Atlas provides two kinds of Triggers: Database and Scheduled triggers.

Note

Triggers are only available for MongoDB Atlas clusters that are running MongoDB version 3.6 or newer.

Database Triggers

Database triggers allow you to execute server-side logic whenever a document is added, updated, or removed in a linked cluster.

Use database triggers to implement complex data interactions, including updating information in one document when a related document changes or interacting with an external service when a new document is inserted.

Database triggers use MongoDB change streams to listen for changes in watched collections and map them to database events.

Scheduled Triggers

Scheduled triggers allow you to execute server-side logic on a regular schedule that you define using CRON expressions. Use scheduled triggers to do work that happens on a periodic basis, such as updating a document every minute, generating a nightly report, or sending an automated weekly email newsletter.

MongoDB Realm Functions Provide Server-side Logic

Triggers execute a MongoDB Realm Function that you specify. Each trigger is associated with exactly one MongoDB Realm Function.

When you create a trigger, you will see a Function editor where you can write the JavaScript code to be executed by the trigger. This Function exists in a shared MongoDB Realm app called Triggers_RealmApp, which Atlas creates automatically for you.

To find this app, click MongoDB Realm in the navigation and select the Triggers_RealmApp app:

Note

Triggers created prior to 09 Jun 2020 use the name Triggers_StitchApp.

Create a Trigger

To create a new database or scheduled trigger:

  1. Click Triggers in the left-hand navigation.
  2. On the Overview tab of the Triggers page, click Add Trigger to open the trigger configuration page.
  3. Enter configuration values for the trigger and click Save at the bottom of the page.

Restart a Suspended Trigger

Triggers may enter a suspended state in response to an event that prevents the trigger’s change stream from continuing, such as a network disruption. When a trigger is suspended, it does not receive change events and will not fire.

Note

In the event of a failed trigger, MongoDB Realm sends an email notification to the project owner, alerting them of the issue.

To restart a suspended trigger:

External Dependencies

An external dependency is an external library that includes logic you’d rather not implement yourself, such as string parsing, convenience functions for array manipulations, and data structure or algorithm implementations.

You can upload external dependencies from the npm repository to MongoDB Realm and then import those libraries into your functions using standard JavaScript module syntax. To use external dependencies in the functions run by your triggers, see External Dependencies.

Configure a Trigger

To configure a Database trigger, see Database Trigger Configuration.

To configure a Scheduled trigger, see Scheduled Trigger Configuration.

1

Find the Suspended Trigger

On the Database Triggers tab of the Triggers page, find the trigger that you want to resume in the list of triggers. Suspended triggers are marked with a Status of Suspended.

../_images/suspended-db-trigger.png
2

Restart the Trigger

Click Restart in the trigger’s Actions column.

You can choose to restart the trigger with a change stream resume token or open a new change stream. Indicate whether or not to use a resume token and then click Resume Database Trigger.

Resume Tokens

If you use a resume token, Atlas attempts to resume the trigger’s underlying change stream at the event immediately following the last change event it processed. If successful, the trigger processes any events that occurred while it was suspended.

If you do not use a resume token, the trigger begins listening for new events but will not fire for any events that occurred while it was suspended.

../_images/resume-database-trigger-modal.png