liveBook · Manning

Atlas Database Triggers

As explained in MongoDB 8.0 in Action, Third Edition

Atlas Database Triggers are MongoDB Atlas automation mechanisms that execute server-side logic in response to changes in a cluster's data or other configured events.

Atlas Database Triggers run server-side logic when data changes in a connected MongoDB Atlas cluster. They can automate synchronization, logging, notifications, and document updates without a separate application server. They are an Atlas feature, not a feature of the core MongoDB server. section 19.0 section 12.5

A database trigger watches changes through a change stream and passes each matching event object to its linked function. It can watch a collection, a database, or an entire cluster. Unlike a SQL trigger running on the database server, its function runs on a separate serverless layer that scales independently. section 19.2.1

To configure a trigger for automatic embedding updates in the Atlas UI:

  • Open Services → Triggers, create a trigger, and select the cluster, sample_mflix database, and embedded_movies collection. section 12.5
  • Select Insert, Update, and Replace Document; enable Full Document so the function can read the changed document. section 12.5
  • Add a JavaScript function that uses the change event to generate or update the document’s embedding. Configure its logic for the document changes it should handle. section 12.5

Keep an embedding current

Insert a document containing a plot into embedded_movies, then query that document to check for its plot_embedding field. The example uses an external embedding model, such as the OpenAI embeddings API, to produce the vector. section 12.5

Choose operations deliberately; $match can filter change events, while $project can restrict event data. Atlas limits database triggers according to cluster size and its change-stream limit, though triggers on the same collection share one stream. Scheduled and authentication triggers handle different kinds of events. section 19.2.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains event delivery, execution scope, filtering, limits, and UI configuration, with a JavaScript embedding-function example.
Examples
Uses an embedded_movies trigger and an inserted plot document whose resulting plot_embedding is checked with a query.
What it assumes
Assumes an Atlas cluster and collection for setup; walks through choosing the watched data and operations.
What you can do after
Operate/configure it in the Atlas UI to invoke a JavaScript function on selected collection changes.
How it connects
Connects database triggers to change streams, serverless functions, MongoDB Atlas Vector Search, and external embedding models.

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.