Docs Menu
Docs Home
/
Relational Migrator
/ /

Convert Triggers

On this page

  • About this Task
  • Before you Begin
  • Steps
  • Example
  • Learn More

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.

  • 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:

    Icon
    Description
    Spinner
    The conversion is now being executed.
    Green check mark
    The conversion was successful.
    Red exclamation mark
    The conversion failed on last attempt.
  • 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.

1

From the Code Generation tab, click the Query Converter pane.

If it is your first time accessing Query Converter or your session has expired, click Log In To Use Query Converter and provide your Atlas credentials.

2
  • If it is your first time using the query converter in your project, click Import From Database.

  • If your project already has converted SQL code, click the Manage Database Objects button on the left pane.

3
  1. On the Import Database Objects modal, click the icon next to Database.

  2. Click the icon next to your schema.

  3. 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.

  4. 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.

4
  1. 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.

  2. 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.

  3. Click the icon on the Converted MongoDB Query pane to copy the MongoDB code to your clipboard.

5
  1. Login to your Atlas account.

  2. From the Overview screen, click Triggers.

  3. 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
  4. Enter a name for the trigger in the Name text field.

  5. Select the Cluster Name, the Database Name and the Collection Name.

  6. Select the Operation Type as Insert Document.

  7. Toggle the Document Preimage and Full Document switches to on.

  8. Enter converted MongoDB code in the Function text field.

    Important

    Update the clusterName and databaseName in the generated Atlas code to match your cluster and database name.

  9. Click Save to save the Atlas 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://cloud.mongodb.com) and choose Triggers
// For more on Atlas triggers see the docs: https://www.mongodb.com/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 } });
}
};
  • Convert Views

  • Convert Queries

  • Convert Stored Procedures

Back

Convert Stored Procedures