BlogAnnounced at MongoDB.local NYC 2024: A recap of all announcements and updatesLearn more >>
MongoDB Developer
Atlas
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Atlaschevron-right

Migrate From an RDBMS to MongoDB With the Help of AI: An Introduction to Query Converter

Nic Raboy4 min read • Published May 02, 2024 • Updated May 02, 2024
SQLAtlas
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Migrating your applications between databases and programming languages can often feel like a chore. You have to export and import your data, transfer your schemas, and make potential application logic changes to accommodate the new programming language or database syntax. With MongoDB and the Relational Migrator tool, these activities no longer need to feel like a chore and instead can become more automated and streamlined.
Query Converter animated
In this article, we'll explore taking SQL queries from your existing application and leveraging the Query Converter functionality built into the Relational Migrator tool to generate drop-in replacements that are MongoDB-compatible.

Prerequisites

Depending on your goals with this particular article, you can follow along with just the Relational Migrator tool as it contains sample schemas that will work for experimentation. However, if you want to play around with your own data, you can connect to one of the popular relational database management systems (RDBMS).

Generate MongoDB queries with the help of AI

Open Relational Migrator and choose to create a new project. For the sake of this article, we'll click "Use a sample schema" to play around. Running queries and looking at data is not important here. We only want to know our schema, our SQL queries, and what we'll end our adventure with query-wise.
Relational Migrator new project
The sample schema we plan to use contains customers and purchase information for those customers. In Relational Migrator, this is referred to as the northwind database.
Relational Migrator erd model
For this example, we want to convert our queries and not focus on the migration of data or schemas. With this in mind, choose the "Code Generation" tab, followed by the "Query Converter" tab.
Relational Migrator Query Converter tab
Now, we can have some fun!
We can take queries from our application and use the AI features of the tool to return the MongoDB equivalent in the programming language of our choosing.
We'll start small. In the "SQL Query" field, add the following:
We know that the above query contains a projection of columns to include, a table to query against, and a condition to filter which data should be returned. If we chose to convert to JavaScript, we'd get a response that looks something like the following:
Had we wanted to, we could have used an asterisk for the projection to include all columns, but we know as seasoned database users that for best performance, we should return only what we need.
Let's take things to the next level with our SQL-to-MongoDB conversions.
Take the following SQL query, for example:
The above query contains a projection, but it also contains an aggregation through the use of the GROUP BY operator. When running the query through the Query Converter, we end up with a result like the following:
Notice that this time, we don't have a .find operation but an .aggregate operation. The Query Converter knew to use a MongoDB aggregation pipeline to get the job done.
Alright, so we're getting a little more complex in how we engage with our relational database.
Let's continue to push our querying capability. Include the following SQL query in the Query Converter tool:
The above SQL query has a projection, we're joining three different tables, and we are grouping the results.
When executing the conversion, we'll get a result that looks like the following:
When using the Query Converter functionality of the Relational Migrator tool, it is important to note a few things:
  • While results might be perfect or close to perfect a majority of the time, depending on the complexity of your queries, you should always test the results before they enter a production environment.
  • The conversion process might not be immediate. Expect that certain queries could take a few minutes to convert.
  • With AI-generated code, the results might differ between generations, even when using the same input criteria as shown in this article.
Having AI available could be a tremendous help if you have limited working knowledge of MongoDB, but have it as part of your future plans.

Conclusion

You received a taste of what is possible when it comes to migrating from a relational database to MongoDB. In particular, we saw how to use the Query Converter, a new AI helper that is bundled with the Relational Migrator tool. Using Relational Migrator can add joy to your migration process not just from a data perspective, but from a querying and application layer perspective as well.
Want to continue the conversation? Head to the MongoDB Developer Community.

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Article

Using Atlas Data Federation to Control Access to Your Analytics Node


Jun 28, 2023 | 9 min read
Tutorial

How to Build a RAG System With LlamaIndex, OpenAI, and MongoDB Vector Database


Feb 16, 2024 | 10 min read
Tutorial

Building AI Graphs with Rivet and MongoDB Atlas Vector Search to Power AI Applications


Apr 02, 2024 | 10 min read
Tutorial

RAG Series Part 1: How to Choose the Right Embedding Model for Your Application


Apr 15, 2024 | 16 min read
Table of Contents
  • Prerequisites