Navigation

Database Triggers

Database Triggers allow you to execute server-side logic whenever a document is added, updated, or removed in a linked MongoDB Atlas cluster. You can use database Triggers to implement complex data interactions, including updating information in one document when a related document changes or interacting with a service upon the insertion of a new document.

Database Triggers use MongoDB change streams to listen for changes to documents in a collection and pass database events to their associated Trigger function.

Important
Change Stream Limitations

MongoDB Realm opens a single change stream for each collection with at least one enabled Trigger and limits the total number of open change streams on each linked cluster across all Realm apps based on the cluster's size. See change stream limitations for more information.

Note

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

Database 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 or change to the underlying cluster. When a Trigger enters a suspended state, it does not receive change events and will not fire.

Note

In the event of a suspended or failed trigger, Realm sends the project owner an email alerting them of the issue.

You can attempt to restart a suspended Trigger from the Realm UI or by importing an application directory with realm-cli.

Database change events represent individual changes in a specific collection of your linked MongoDB Atlas cluster.

Every database event has the same operation type and structure as the change event object that was emitted by the underlying change stream. Change events have the following operation types:

Operation Type
Description
INSERT
Represents a new document added to the collection.
UPDATE
Represents a change to an existing document in the collection.
REPLACE
Represents a new document that replaced a document in the collection.
DELETE
Represents a document deleted from the collection.

Database change event objects have the following general form:

{
_id : <ObjectId>,
"operationType": <string>,
"fullDocument": <document>,
"fullDocumentBeforeChange": <document>,
"ns": {
"db" : <string>,
"coll" : <string>
},
"documentKey": {
"_id": <ObjectId>
},
"updateDescription": <document>,
"clusterTime": <Timestamp>
}
Give Feedback

On this page