Martini Invoking Services via a MongoDB Change Stream 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.
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. |
Usage Example
To set up a MongoDB Change Stream trigger:
- Navigate to the Triggers configuration in Martini.
- Select
New Trigger > MongoDB Change Stream
. - Fill in the necessary fields based on the configuration table above.
- Implement a Martini service that will handle the incoming data based on the MongoDB changes.
Service Parameters
When a MongoDB change stream event occurs, the following parameters are passed to the invoked Martini service:
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.