|
| 1 | +.. _rm-convert-queries: |
| 2 | + |
| 3 | +=============== |
| 4 | +Convert Queries |
| 5 | +=============== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +You can convert embedded application and reporting SQL queries to MongoDB |
| 14 | +syntax using the query converter. Copy and paste your queries into the |
| 15 | +query converter to update them to work with MongoDB and your migrated |
| 16 | +schema. |
| 17 | + |
| 18 | +About this Task |
| 19 | +--------------- |
| 20 | + |
| 21 | +.. include:: /includes/fact-query-converter-generic.rst |
| 22 | + |
| 23 | +- .. include:: /includes/fact-query-converter-disclaimer.rst |
| 24 | + |
| 25 | +Steps |
| 26 | +----- |
| 27 | + |
| 28 | +.. procedure:: |
| 29 | + :style: normal |
| 30 | + |
| 31 | + .. step:: Navigate to the query converter pane |
| 32 | + |
| 33 | + From the :guilabel:`Code Generation` tab, click the |
| 34 | + :guilabel:`Query Converter` pane. |
| 35 | + |
| 36 | + .. step:: Open the SQL query converter view |
| 37 | + |
| 38 | + - If it is your first time using the query converter in your |
| 39 | + project, click :guilabel:`Paste SQL Query`. |
| 40 | + - If you have previously converted SQL code in your project, click the |
| 41 | + :guilabel:`+ ADD` button on the left-hand pane next to |
| 42 | + :guilabel:`Queries`. |
| 43 | + |
| 44 | + .. step:: Enter your SQL query |
| 45 | + |
| 46 | + a. Copy your SQL query to your clipboard. |
| 47 | + |
| 48 | + .. note:: |
| 49 | + |
| 50 | + Queries copied into the :guilabel:`SQL Query` text field |
| 51 | + must be a SELECT query. For example: |
| 52 | + ``SELECT ID,ITEMNAME,PRICE FROM inventory``. |
| 53 | + |
| 54 | + #. Under the :guilabel:`SQL Query` header, paste your SQL query |
| 55 | + into the text box. |
| 56 | + |
| 57 | + .. step:: Convert and test syntax |
| 58 | + |
| 59 | + a. Click the :guilabel:`Convert` button. Wait for your SQL code to |
| 60 | + converted. The converted MongoDB syntax code displays |
| 61 | + in the :guilabel:`Converted MongoDB Query` text box. |
| 62 | + |
| 63 | + .. tip:: |
| 64 | + |
| 65 | + You can rename or delete queries from your project |
| 66 | + by clicking the :icon-fa4:`ellipsis-h` |
| 67 | + button on the :guilabel:`SQL Query` pane and clicking the |
| 68 | + :icon-fa5:`pencil-alt` or :icon-fa4:`trash` icon. |
| 69 | + |
| 70 | + #. Click the :icon-fa5:`copy` icon to copy the MongoDB syntax |
| 71 | + to your clipboard. |
| 72 | + #. Test and validate the generated MongoDB syntax. |
| 73 | + |
| 74 | + .. tip:: |
| 75 | + |
| 76 | + .. include:: /includes/fact-query-converter-filter.rst |
| 77 | + |
| 78 | +Examples |
| 79 | +-------- |
| 80 | + |
| 81 | +The following table shows examples of SQL queries converted to MongoDB |
| 82 | +syntax using query converter. The syntax and structure of the converted |
| 83 | +queries vary depending on the mapping rules used in the project: |
| 84 | + |
| 85 | +.. list-table:: |
| 86 | + :header-rows: 1 |
| 87 | + :widths: 40 60 |
| 88 | + |
| 89 | + * - SQL Syntax |
| 90 | + - MongoDB Syntax |
| 91 | + |
| 92 | + * - |
| 93 | + .. code-block:: sql |
| 94 | + :copyable: false |
| 95 | + |
| 96 | + SELECT * |
| 97 | + FROM CUSTOMERS |
| 98 | + - |
| 99 | + .. code-block:: |
| 100 | + :copyable: false |
| 101 | + |
| 102 | + async function query(db) { |
| 103 | + return await db.collection('Customers').find({ |
| 104 | + }).toArray(); |
| 105 | + } |
| 106 | + |
| 107 | + * - |
| 108 | + .. code-block:: sql |
| 109 | + :copyable: false |
| 110 | + |
| 111 | + SELECT CONTACTNAME, CITY |
| 112 | + FROM CUSTOMERS |
| 113 | + WHERE CONTACTNAME LIKE '%SMITH%' |
| 114 | + |
| 115 | + - |
| 116 | + .. code-block:: node |
| 117 | + :copyable: false |
| 118 | + |
| 119 | + async function query(db) { |
| 120 | + return await db.collection('Customers').find({ |
| 121 | + ContactName: { $regex: '.*SMITH.*' } |
| 122 | + }, { |
| 123 | + projection: { ContactName: 1, City: 1, _id: 0 } |
| 124 | + }).toArray(); |
| 125 | + } |
| 126 | + |
| 127 | + * - |
| 128 | + .. code-block:: sql |
| 129 | + :copyable: false |
| 130 | + |
| 131 | + SELECT CUSTOMERID, CITY |
| 132 | + FROM CUSTOMERS AS C |
| 133 | + JOIN ORDERS AS O |
| 134 | + ON C.CUSTOMERID = O.CUSTOMERID |
| 135 | + WHERE CONTACTNAME IN('ABI','JIM') |
| 136 | + |
| 137 | + - |
| 138 | + .. code-block:: node |
| 139 | + :copyable: false |
| 140 | + |
| 141 | + const query = async (db) => { |
| 142 | + return await db.collection('Customers').aggregate([ |
| 143 | + { |
| 144 | + $lookup: { |
| 145 | + from: 'Orders', |
| 146 | + localField: 'CustomerId', |
| 147 | + foreignField: 'CustomerId', |
| 148 | + as: 'customer_orders' |
| 149 | + } |
| 150 | + }, |
| 151 | + { |
| 152 | + $match: { |
| 153 | + CONTACTNAME: { $in: ['ABI', 'JIM'] } |
| 154 | + } |
| 155 | + }, |
| 156 | + { |
| 157 | + $project: { |
| 158 | + CustomerId: 1, |
| 159 | + City: 1 |
| 160 | + } |
| 161 | + } |
| 162 | + ]).toArray(); |
| 163 | + }; |
| 164 | + |
| 165 | +Next Steps |
| 166 | +---------- |
| 167 | + |
| 168 | +- :ref:`rm-convert-views` |
| 169 | +- :ref:`rm-convert-stored-procedures` |
| 170 | + |
| 171 | +Learn More |
| 172 | +---------- |
| 173 | + |
| 174 | +- :ref:`sql-to-mongodb-mapping` |
| 175 | +- :manual:`SQL to Aggregation Mapping Chart </reference/sql-aggregation-comparison>` |
0 commit comments