Convert Triggers
You can import and convert your SQL triggers to MongoDB Atlas Triggers with the query converter. The query converter considers the SQL code and relational schema defined in your project when converting your triggers.
About this Task
The query converter uses AI technology which may not be able to convert long or complex queries, triggers, or stored procedures. Some queries may not be converted correctly while others may not be converted at all.
The query converter uses the relational schema, the MongoDB schema, and the mapping rules in your current project to determine how the queries should be converted. Conversions may fail or be incorrect if the queries reference tables that are not in your relational schema or if they are not mapped to MongoDB collections.
Converted queries, triggers, views, and stored procedures are saved in your project and persist through project import and exports.
SQL queries are limited to 40,000 text characters.
You can view the history of previous conversions in the left-hand Query Converter pane. Each conversion has an icon indicating the result of the conversion. If an object does not have an icon next to it, a conversion has not been attempted yet:
IconDescriptionSpinnerThe conversion is now being executed.Green check markThe conversion was successful.Red exclamation markThe conversion failed on last attempt.
Before you Begin
Your relational database must have at least one trigger to convert.
Always review and test the code generated by query converter before deploying it in a production environment.
Steps
Select triggers
On the Import Database Objects modal, click the icon next to Database.
Click the icon next to your schema.
Click the icon next to Triggers.
Tip
To toggle a trigger for conversion, click the icon next to the trigger's name. All triggers are selected by default.
Click Save.
The code for each trigger in your database schema is imported into your project and is visible in the left Query Converter pane under Triggers.
Convert the SQL trigger
Click a trigger's name from the left pane under Triggers.
Tip
You can use the Filter text box to filter queries, stored procedures, triggers, and views based on object name and SQL syntax.
The SQL trigger code displays in the Imported Trigger pane.
Click the Convert button. Wait for the query converter to convert your code.
The converted MongoDB code displays on the Converted MongoDB Query pane.
If the query converter has errors, you can view the details in the Converted MongoDB Query pane.
Click the icon on the Converted MongoDB Query pane to copy the MongoDB code to your clipboard.
Create the Trigger in Atlas
Login to your Atlas account.
From the Overview screen, click Triggers.
Click Add Trigger.
Tip
The converted MongoDB code contains commented lines for all the variables you must select in Atlas to create your trigger. For example:
// Collection Name: products // Operation Type: Insert Enter a name for the trigger in the Name text field.
Select the Cluster Name, the Database Name and the Collection Name.
Select the Operation Type as Insert Document.
Toggle the Document Preimage and Full Document switches to on.
Enter converted MongoDB code in the Function text field.
Important
Update the
clusterName
anddatabaseName
in the generated Atlas code to match your cluster and database name.Click Save to save the Atlas trigger.
Example
Convert a MySQL Trigger
The following example shows a MySQL trigger converter to Atlas:
CREATE TRIGGER TRIGGER_UPPER_PRODUCTS BEFORE INSERT ON MYDATABASE.PRODUCTS FOR EACH ROW SET NEW.FULL_NAME = UPPER(new.FULL_NAME)
// The relational database trigger has been converted to MongoDB Atlas Triggers format. // To create a trigger, open your Atlas project (https://mongodb.prakticum-team.ru/proxy/cloud.mongodb.com) and choose Triggers // For more on Atlas triggers see the docs: https://mongodb.prakticum-team.ru/docs/atlas/triggers/ // Create your trigger using the following settings and paste the code into the Function section: // Watch Against: Collection // Cluster Name: Ensure clusterName matches selection in Atlas Trigger configuration // Database Name: Ensure databaseName matches selection in Atlas Trigger configuration // Collection Name: products // Operation Type: Insert // Full Document: On // Document Preimage: Off exports = async function(changeEvent) { const clusterName = "clusterName"; const databaseName = "databaseName"; const { fullDocument } = changeEvent; const db = context.services.get(clusterName).db(databaseName); const collection = db.collection('products'); if (fullDocument && fullDocument.fullName) { fullDocument.fullName = fullDocument.fullName.toUpperCase(); await collection.updateOne({ _id: fullDocument._id }, { $set: { fullName: fullDocument.fullName } }); } };