Migrate from MongoDB to SQL

Turn collections into relational tables, as SQL files or directly in MySQL, SQL Server, PostgreSQL, Oracle or IBM DB2, with embedded objects and arrays mapped to columns or child tables.

The MongoDB → SQL migration turns collections into relational tables. A unit maps one collection or view. Its mapping tree says where each field goes: a column, columns of the same table, or a child table linked by keys. The output is either SQL files in a folder or tables created directly on a SQL server.

The MongoDB source is only read. The SQL target is written only by a run, after a confirmation that names every unit, every file that will be replaced and every table that will be dropped.

Targets

Target kindWritesDialects
DirectoryOne SQL file per unit (CREATE TABLE and INSERT INTO statements)MySQL, Microsoft SQL Server, PostgreSQL, Oracle, IBM DB2
ServerTables on a saved SQL connectionMySQL, Microsoft SQL Server, PostgreSQL, Oracle, IBM DB2 (the type follows the server)

Sybase is a source of the SQL → MongoDB migration only, not a target. Oracle and IBM DB2 need their client installed (Edit › Preferences › External tools › SQL Drivers).

Run a migration

Example: write acme_store.orders to a PostgreSQL database.

  1. Right-click orders in the tree and choose SQL Migration › MongoDB -> SQL Migration. You can also use the SQL Migration dropdown on the global toolbar. The collection becomes the first export unit.
  2. Check MongoDB source connection: (use (Switch connection) to change it).
  3. Next to SQL target connection/directory:, choose Server and pick the SQL connection, or choose Directory and a folder.
  4. Add export units adds more collections. Each one is scanned for a default mapping.
  5. In the Overview, check each unit’s Target database (or Target file), Mapping and Drop Table Mode. Untick a unit to leave it out of Run migration.
  6. Select a unit and click Mappings to adjust its tables (next section).
  7. Click Run migration or Run selected units, and confirm.

The migration always creates its own tables. An existing table with the same name stops the unit, unless its Drop Table Mode is Drop existing tables. That choice is named in the confirmation.

Edit a unit’s mapping

The mapping tab shows the document tree on the left and the SQL tables it produces on the right (SQL Table Preview, SQL Statement Preview).

  • Select a mapping and use Edit Mapping… to change its field, mapping type, relationship, table and column names, and SQL type.
  • Include in export (Ctrl+I) turns a field on or off.
  • Add child (Alt+K) and Add sibling (Alt+J) add mappings for fields the scan did not see.
  • Use as primary key makes _id (or another value of the main table) the primary key. Otherwise an automatic key is used.
  • New mapping (Ctrl+T) replaces the mappings with a new default, from a fresh scan of the documents (Number of documents to scan).
  • Update statement preview refreshes the statements. Auto update statement preview does it after every change.

Mapping types

Mapping TypeUse for
ColumnA plain field mapped to its own column
Object FieldsAn embedded object, mapped to SQL
Array Elements To RowsAn array, one row per element, with an index column
Array Elements To ColumnsAn array, one column per position

Relationship types

Relationship TypeWhere the data goes
EmbedColumns added to the table of a parent mapping
Table, PK <- FKA new table, with a foreign key to the parent’s primary key
Table, FK -> PKA new table, and the parent table gets a foreign key to it
Table, No LinkA new table, without a link to the parent

Example: in orders, map items (an array of objects) as Array Elements To Rows with Table, PK <- FK. You get an orders table and an orders_items table that references it.

SQL types

Edit SQL types shows the default conversion from each MongoDB type to each SQL dialect. You can change these conversions or add custom ones. Automatically set type of known fields applies them to new mappings.

Monitored runs and Revert

On a unit’s mapping tab, Run monitored export runs that unit and keeps its log in SQL Export Log Messages. If the result is wrong, Revert … undoes the unit’s finished or interrupted run: it drops the tables it created, or deletes the file it wrote. It always asks first. View / save migration logs opens or copies a unit’s log.

Options reference

OptionValues
SQL target kindServer, Directory
Drop Table Mode (per unit)Do not drop (default), Drop existing tables
Number of documents to scanHow many documents a new mapping reads
Primary keyUse as primary key, or Use automatic primary key (Primary key name, Primary key initial value)
Incremental execution optionsExport only the documents added since the last run of the saved task

Save mappings and Load mappings keep the units and their mappings in a file. Save task keeps the migration as a task (Automation).

Tips

  • Start with a Directory target and read the generated SQL before writing to a server.
  • Use Show insert statements for one preview document only to follow one document through every table.

Troubleshooting

ProblemWhat to do
A unit stops because a table existsChoose Drop existing tables for that unit, or another target database.
A field warns about a type mismatchIts values have several types. Change the SQL type, or untick Warn when encountering a type mismatch.
“Sybase is a SQL → MongoDB source only”Choose another target type.
An existing file was replacedThe confirmation lists replaced files. Files are written next to the target and renamed into place only when complete.