Explore Developer Center's New Chatbot! MongoDB AI Chatbot can be accessed at the top of your navigation to answer all your MongoDB questions.

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 Aug 28, 2024 • Updated Aug 28, 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:
1SELECT contact_name, country
2FROM customers
3WHERE country = 'USA'
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:
1async function query(db) {
2 return await db.collection('customers').find({ country: 'USA' }, { projection: { contactName: 1, country: 1, _id: 0 } }).toArray();
3}
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:
1SELECT country, COUNT(*) AS total
2FROM customers
3GROUP BY country
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:
1async function query(db) {
2 return await db.collection('customers').aggregate([
3 { $group: { _id: "$country", total: { $sum: 1 } } }
4 ]).toArray();
5}
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:
1SELECT customers.contact_name, SUM(order_details.unit_price) AS spent
2FROM orders
3JOIN customers ON orders.customer_id = customers.customer_id
4JOIN order_details ON orders.order_id = order_details.order_id
5JOIN products ON order_details.product_id = products.product_id
6GROUP BY customers.contact_name
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:
1async function query(db) {
2 return await db.collection('orders').aggregate([
3 {
4 $lookup: {
5 from: 'customers',
6 localField: 'customerId',
7 foreignField: 'customerId',
8 as: 'customer'
9 }
10 },
11 {
12 $unwind: '$customer'
13 },
14 {
15 $lookup: {
16 from: 'orderDetails',
17 localField: 'orderId',
18 foreignField: 'orderId',
19 as: 'orderDetail'
20 }
21 },
22 {
23 $unwind: '$orderDetail'
24 },
25 {
26 $lookup: {
27 from: 'products',
28 localField: 'orderDetail.productId',
29 foreignField: 'productId',
30 as: 'product'
31 }
32 },
33 {
34 $unwind: '$product'
35 },
36 {
37 $group: {
38 _id: '$customer.contactName',
39 spent: {
40 $sum: '$orderDetail.unitPrice'
41 }
42 }
43 },
44 {
45 $project: {
46 _id: 0,
47 contactName: '$_id',
48 spent: 1
49 }
50 }
51 ]).toArray();
52}
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.
Top Comments in Forums
There are no comments on this article yet.
Start the Conversation

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

Caching LLMs Response With MongoDB Atlas and Vector Search


Sep 02, 2024 | 8 min read
Article

Harnessing Natural Language for MongoDB Queries With Google Gemini


Dec 13, 2024 | 8 min read
Tutorial

Tutorial: Build a Movie Search Engine Using Atlas Full-Text Search in 10 Minutes


Sep 09, 2024 | 10 min read
Article

AI Shop: The Power of LangChain, OpenAI, and MongoDB Atlas Working Together


Sep 18, 2024 | 7 min read
Table of Contents
  • Prerequisites