liveBook · Manning

SQL trigger

As explained in MongoDB 8.0 in Action, Third Edition

A database trigger responds to changes in database data, such as modifications to documents in a collection.

A database trigger runs logic automatically when a defined event occurs. The implementation here uses MongoDB Atlas rather than SQL: a database trigger watches a collection for document inserts, updates, deletes, or replacements. No SQL trigger syntax is specified. section 19.2.1 section 19.3

Atlas uses change streams to track collection changes. When an event meets a trigger’s criteria, the trigger passes the event object to an associated Atlas Function. That server-side JavaScript function performs the work, such as updating related data or calling another service. section 19.3 section 19.2.1

To set up the Atlas version:

  • Choose a Database trigger for collection changes; Scheduled and Authentication triggers respond to different kinds of events. section 19.2.1
  • In Add Trigger, name the trigger and set its enabled state. The interface also offers controls for skipping events when reenabled and for event ordering. section 19.2.1
  • Connect a function that handles the event object. For debugging, inspect the change event and check fields such as operation type and document key before acting. section 19.2.1 section 19.3

Enrich a new document

A trigger can call an Atlas Function after a document is inserted. The function can call an external embedding API and store the resulting vector alongside the original document, avoiding a separate manual enrichment step. 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 the controls used to name, enable, and configure an Atlas trigger. section 19.2.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains event-to-function flow and setup controls, but provides no SQL trigger statements or SQL-dialect implementation.
Examples
Illustrates related-record updates, inventory adjustments, document embeddings, and a function that logs change events.
What it assumes
Assumes an Atlas collection to monitor and uses server-side JavaScript for the associated function.
What you can do after
Operate/configure it in Atlas by selecting a database trigger, setting its controls, and connecting an event-handling function.
How it connects
Connects database triggers to change streams, Atlas Functions, scheduled triggers, and authentication triggers.

Also explained in

Compare how 4 books explain SQL 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.