Docs Menu

Configure Database Triggers

On this page

  • Database Triggers
  • Scheduled Triggers
  • Atlas Functions Provide Server-side Logic
  • Create a Trigger
  • Restart a Suspended Trigger
  • Find the Suspended Trigger
  • Restart the Trigger
  • External Dependencies
  • Configure a Trigger

Atlas 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.

Triggers in the MongoDB architecture
click to enlarge

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.

Note

MongoDB Atlas performs a replace command rather than an update command when executing an update via the Atlas UI. Database triggers will only recognize this update via the Atlas UI if you have marked the replace database event for the trigger.

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.

Triggers execute a function that you specify. Each trigger is associated with exactly one 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 App Services app called Triggers, which Atlas creates automatically for you.

To find this app, click App Services in the navigation and select the Triggers app.

Note

Triggers created between 09 June 2020 and 01 June 2022 use the name Triggers_RealmApp. Triggers created prior to 09 June 2020 use the name Triggers_StitchApp.

To create a new database or scheduled trigger:

  1. Click the Atlas tab in the top navigation of your screen if you have not already navigated to Atlas.
  2. Click Triggers in the left-hand navigation.
  3. On the Overview tab of the Triggers page, click Add Trigger to open the trigger configuration page.
  4. Enter configuration values for the trigger and click Save at the bottom of the page.

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, Atlas App Services sends an email notification to the project owner, alerting them of the issue.

To restart a suspended trigger:

1

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.

The list of triggers on the Database Triggers tab of the Triggers page.
2

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.

Note
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.

Select the checkbox to restart a trigger with a resume token.

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 App Services 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.

To configure a Database trigger, see Database Trigger Configuration.

To configure a Scheduled trigger, see Scheduled Trigger Configuration.

←  Run Aggregation PipelinesTrigger Configuration Parameters →
Give Feedback
© 2022 MongoDB, Inc.

About

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