MongoDB Triggers

Mayur Wadekar
4 min readDec 20, 2021

Triggers are generally used to execute the server-side logic. In MongoDB, Triggers allow you to execute server-side logic in response to database events or according to a schedule.

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

MongoDB

Triggers execute a MongoDB Realm Function that you specify. Each trigger is related to exactly one MongoDB Realm Function.

there are two types of triggers in MongoDB:

  1. Database Trigger: Database triggers come into the picture when you wanted to execute server-side logic for any database operations like add, update or delete documents in an existing cluster. As database triggers are generally used to perform complex operations like when one document changes/updates then related documents might also be getting updated or if one document is deleted then you might interact with external clients.
  2. Scheduled Trigger: Scheduled triggers come into the picture when you wanted to do work that happens on a periodic basis. For example, any report generation on daily basis, sending email newsletters, deleting or updating some data before a particular timeframe, etc.

How To Create Triggers

To create triggers you have to follow the following steps.

  1. After login into your atlas account click the Atlas tab in the top navigation of your screen.
  2. Click on the triggers link on the left-hand side navigation bar.
  3. Click Add Trigger to open the trigger configuration page.
  4. Enter configuration values for the trigger and click on the Save at the rock bottom of the page.
How To Create Triggers

Configurations For Database Triggers

  1. Trigger Type: Set to Database to make a Database trigger.
  2. Name: Name of the trigger.
  3. Enabled: Trigger is enabled or not. By default is enabled.
  4. Event Ordering: multiple executions of this trigger will occur sequentially supported by the timestamps of the change events.
  5. Enabled Clusters: The names of one or more MongoDB Atlas clusters.
  6. Link Data Source: The trigger listens for events in a collection within this cluster.
  7. Database and Collection Name
  8. Operation Type: One or more database operation types that cause the trigger to fire. ex. INSERT, UPDATE, DELETE, REPLACE, etc.
  9. Full Document: If enabled then the UPDATE event includes the most recent read full document.
  10. Function: A MongoDB Realm Function, written in JavaScript.

Update operations executed from MongoDB Compass or the MongoDB Atlas Data Explorer fully replace the previous document.

Configurations For Scheduled Triggers

Many fields are the same as above and the main difference for it is we can schedule a trigger using the following option.

  1. Schedule Type: There are two types of mode has provided. One is basic and the second is advanced. In Basic mode, there are some predefined values in a dropdown for scheduling the trigger. In Advanced mode, a CRON expression determines when to fireside the trigger.

Trigger Functions

When you write a trigger function you should possibly have to note the following points.

  1. A Database Trigger will always call a function with a changeEvent.
  2. A Scheduled Trigger will always call a function without arguments.
  3. Functions run by Triggers are run as System users and have full access to Services, Functions, and MongoDB Data.
  4. To access the MongoDB database service you have to connect MongoDB with the following first and then you can directly write operations over it.
// Connect to collection
const collection = context.services.get(<CLUSTER_NAME>).db("db_name").collection("coll_name");
const doc = collection.findOne({ databasename: "mongodb" });

5. You can also specify the default HTTP context and execute the get request

const response = context.http.get({ url: <URL> })

6. You can call other named functions if they are defined in your application

const result = context.functions.execute(“function_name”, <arguments>);

7. You can also include the external dependency using the dependency tab. Where you can specify external dependency package name and version.

Require statements must appear inside a function declaration, not outside it. MongoDB Realm doesn’t currently support statements within the global scope.

Add Dependency of External Modules

Database triggers are generally designed to execute really small logic which modifies the database collections. Do not overflow the triggers as it might cause delays in operations for the database as they are executed sequentially.

Hope you’ve enjoyed reading…! :)

--

--