liveBook · Manning

Trigger

As explained in MongoDB 8.0 in Action, Third Edition

A trigger is an event-driven automation mechanism that invokes an action or function when a configured event occurs.

A trigger runs server-side logic when a configured event occurs. In MongoDB Atlas Application Services, it invokes an Atlas Function without a separate application server. Use triggers when data changes or recurring tasks should start work automatically—for example, synchronizing records, sending notifications, or enforcing business rules. section 19.2 section 19.3

Choose the trigger type to match the event:

  • Database: respond to document inserts, updates, deletes, or replacements in a collection. section 19.2.1
  • Scheduled: run a function at predefined intervals, such as for a daily report; advanced scheduling supports cron expressions. section 19.3 section 19.2.1
  • Authentication: respond to user events such as registration, login, or account deletion. section 19.3

Each trigger is connected to a function. When its criteria match, the trigger passes an event object to that function. Database triggers use MongoDB change streams to detect collection changes; the associated JavaScript function supplies the response, including work that calls an external API. section 19.3 section 19.2.1

React to a new document

On insertion, a database trigger can invoke a function that calls the OpenAI Embedding API, then stores the resulting vector alongside the original document. For a simpler first check, log the received change event and verify that it contains an operationType before acting. section 19.2.1

Figure 19.5 The Add Trigger interface in Atlas. At the top, you can choose the trigger type: Database or Scheduled. In the Trigger Details section, you can set the trigger’s name, enable or disable it, choose to skip events when reenabled, and control event ordering. (Image © MongoDB 2025)
A screenshot of a computer

AI-generated content may be incorrect.

The Add Trigger interface shows where to choose a type, name and enable the trigger, and set event-ordering or reenablement behavior. section 19.2.1

Figure 19.6 The interface for setting up a scheduled trigger. Trigger Type is set to Scheduled, indicating that this trigger will execute at regular intervals. Schedule Type is set to Advanced, which allows complex scheduling with cron expressions. cron expressions provide flexibility by allowing you to define when the trigger should run. (Image © MongoDB 2025)

The scheduled-trigger interface shows the Advanced schedule option for defining execution times with a cron expression. section 19.2.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains trigger types, event delivery, configuration controls, and scheduling, but does not detail change-stream internals.
Examples
Illustrates shipping-record synchronization, order-driven inventory updates, insertion-time vector enrichment, and change-event logging.
What you can do after
Operate/configure Atlas triggers to invoke functions for database changes or recurring tasks.
How it connects
Connects triggers to Atlas Functions, MongoDB change streams, cron expressions, and external APIs.

Also explained in

Compare how 9 books explain trigger →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.