MongoDB Listener Trigger
Introduction
The MongoDB Change Stream trigger allows Martini applications to react in real-time to changes in MongoDB databases. It supports a variety of operations, making it versatile for use cases such as data synchronization, auditing, and real-time analytics.
Prerequisites
- MongoDB 4.x, with change streams feature enabled.
- A MongoDB connection configured in Martini.
Usage in Workflows
To learn how to setup and use the MongoDB Listener trigger in workflows, see Martini Workflows Trigger Nodes
Usage in Services
To learn how to setup and use the MongoDB Listener trigger in services, see Invoking Services via a Trigger
Configuration
General Configuration
Ensure you have configured the MongoDB connection in Martini correctly. Refer to the Martini documentation for initial setup instructions.
MongoDB Listener-Specific Configuration
Configure the MongoDB Change Stream trigger using the following properties:
| Name | Default | Description |
|---|---|---|
| Connection Name | (required) | Name of the MongoDB database connection. |
| Database Name | (required) | Name of the MongoDB database to watch for changes. |
| Collection Name | <none> |
Name of the database collection to watch for changes. If no value is specified or this field's value is <none>, all non-system collections are watched. |
| Full Document Lookup | Default |
Determines the data to return for update operations. |
| Keep Alive | true | Whether the endpoint should be stopped or not when an invalidation event occurs (e.g., watched database or collection is dropped). |
| Pipelines | The aggregation pipeline to apply to the listener's change stream. Each pipeline should follow the MongoDB extended JSON format. |
Parameters
When a MongoDB Listener Trigger event occurs, the following parameters are passed to the invoked Martini service or workflow:
| Name | Type | Description |
|---|---|---|
body |
io.toro.model.object.property.Model |
Type of the change. |
databaseName |
java.lang.String |
Name of the database where the change occurred. |
collectionName |
java.lang.String |
Name of the collection where the change occurred. |
namespace |
java.lang.String |
Database and collection name joined by a '.'. |
namespaceDocument |
org.bson.BsonDocument |
Document type of the namespace. |
operationType |
java.lang.String |
Type of change (insert, update, replace, delete, invalidate, drop, dropDatabase, rename, other). |
fullDocument |
io.toro.model.object.property.Model |
The complete changed document. |
documentKey |
org.bson.BsonDocument |
ID of the changed document. |
clusterTime |
java.lang.Long |
Time at which the change occurred. |
updateDescription |
com.mongodb.client.model.changestream.UpdateDescription |
Contains updated/removed fields. |
Best Practices
- Apply filters using the Pipelines configuration to limit the events to those relevant to your application, reducing overhead.
- Monitor the MongoDB and Martini performance to ensure the change streams do not negatively impact your system.
Troubleshooting
If the trigger does not seem to be working: - Verify the MongoDB connection and ensure it is correctly configured in Martini. - Check if the specified database and collection names are correct and exist in your MongoDB instance. - Ensure that your MongoDB user has the necessary permissions to read change streams.