|
| 1 | +import Screenshot from "@site/src/components/Screenshot"; |
| 2 | + |
| 3 | +# 🦸 Testing Converted Queries |
| 4 | + |
| 5 | +You have already imported and converted your source queries, stored procedures or views into MongoDB queries. |
| 6 | +Relation Migrator allows you to go a step further, and verify the converted query by allowing you to compare results from the source query and converted query. |
| 7 | + |
| 8 | +## Prerequisite |
| 9 | +1. In order for Relational Migrator to run converted queries, you need to first [install and run Query Runner](https://www.mongodb.com/docs/relational-migrator/code-generation/query-converter/test-converted-queries/install-query-runner/). |
| 10 | + |
| 11 | +2. The MongoDB query will be executed on your actual MongoDB database, so you must have completed [migrating your data](https://mongodb-developer.github.io/relational-migrator-lab/docs/Migrate%20Data/migration). |
| 12 | + |
| 13 | +2. If you source data resides in the `public` schema in your PostgreSQL database, you may skip this step. Otherwise, you will need to add your source data schema into PostgreSQL's search path in order for the queries execute properly. |
| 14 | + |
| 15 | + Go to **Manage Connections**: |
| 16 | + <Screenshot url="https://www.mongodb.com/products/tools/relational-migrator" src="img/testing-queries-001.png" alt="Screenshot to manage connection" /> |
| 17 | + |
| 18 | + Edit your **JDBC URI** to include the search path. For example, if your source schema is `library`, add `options=-c%20search_path%3Dlibrary` as a query parameter to your URI: |
| 19 | + |
| 20 | + <Screenshot url="https://www.mongodb.com/products/tools/relational-migrator" src="img/testing-queries-002.png" alt="Screenshot to add search path" /> |
| 21 | + |
| 22 | + Click **Save** and you can use the browser's **back button** to navigate back to the Query Converter page. |
| 23 | + |
| 24 | +3. After which, you may expand the test query panel to begin executing your source query or converted query. |
| 25 | + |
| 26 | + <Screenshot url="https://www.mongodb.com/products/tools/relational-migrator" src="img/testing-queries-003.png" alt="Screenshot to expand query test panel" /> |
| 27 | + |
| 28 | +## Executing queries for comparison |
| 29 | + |
| 30 | +1. Select the source query you want to test on the left pane. As an example, we will select the `get_books_by_genre` stored procedure. |
| 31 | +2. Convert the query to a MongoDB query if you have not done so, but note that the converted query may not always be correct. |
| 32 | +3. If it is a stored procedure, you will need to replace the placeholder parameter with a test value. In this example, we will replace it with `'Literature'`. |
| 33 | + <Screenshot url="https://www.mongodb.com/products/tools/relational-migrator" src="img/testing-queries-004.png" alt="Screenshot to show placeholder replacement" /> |
| 34 | +4. Click **RUN SOURCE QUERY** and **RUN CONVERTED QUERY** to execute the queries and compare the results. |
| 35 | + <Screenshot url="https://www.mongodb.com/products/tools/relational-migrator" src="img/testing-queries-005.png" alt="Screenshot to show result comparison" /> |
| 36 | +5. If the query results are different, check your MongoDB query. In this example, your MongoDB query should look like: |
| 37 | + ```js |
| 38 | + async function get_books_by_genre(db, genre_param) { |
| 39 | + const result = await db.collection('books').aggregate([ |
| 40 | + { |
| 41 | + $match: { |
| 42 | + 'genre': genre_param |
| 43 | + } |
| 44 | + }, |
| 45 | + { |
| 46 | + $project: { |
| 47 | + title: 1 |
| 48 | + } |
| 49 | + } |
| 50 | + ]).toArray(); |
| 51 | + return result; |
| 52 | + } |
| 53 | + ``` |
| 54 | + Also check that any changes your made to your MongoDB query are saved. |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
0 commit comments