MongoDB to SQL MigrationMongoDB到SQL的迁移
Posted on: 22/05/2019 (last updated: 10/08/2021) by Stephan Weiss
MongoDB to SQL Migration makes it possible to export single or multiple MongoDB collections to a SQL file or database, and map fields to SQL tables and columns before the export is run.“MongoDB到SQL的迁移”可以将单个或多个MongoDB集合导出到SQL文件或数据库,并在导出运行之前将字段映射到SQL表和列。
Download Studio 3T to try it today.下载Studio 3T,立即试用。
MongoDB to SQL Migration currently supports MySQL, PostgreSQL, Oracle, Microsoft SQL Server, and IBM DB2.MongoDB到SQL的迁移目前支持MySQL、PostgreSQL、Oracle、Microsoft SQL Server和IBM DB2。
Read our guide on migrating data in the opposite direction, SQL to MongoDB Migration.请阅读我们关于向相反方向迁移数据的指南,即SQL到MongoDB的迁移。
Introduction介绍
MongoDB to SQL Migration allows users to export MongoDB collections to a SQL file or database. “MongoDB到SQL迁移”允许用户将MongoDB集合导出到SQL文件或数据库。It is readily integrated with the Tasks feature which lets users automate migration.它很容易与任务功能集成,使用户可以自动迁移。
In a nutshell:简而言之:
Each MongoDB to SQL Migration task allows the configuration of multiple export units.每个MongoDB到SQL迁移任务都允许配置多个导出单元。Each export unit configures the export of data from a MongoDB collection to a SQL file or directly to a (live) SQL database.每个导出单元配置将数据从MongoDB集合导出到SQL文件或直接导出到(实时)SQL数据库。Each export unit uses a fully configurable mapping, allowing full control over the mapping of each field to a column and/or row in SQL.每个导出单元使用完全可配置的映射,允许完全控制每个字段到SQL中的列和/或行的映射。It can preserve relationships between data in a MongoDB document by recreating them using corresponding foreign key constraints.它可以通过使用相应的外键约束重新创建MongoDB文档中的数据之间的关系来保留它们。It always creates and populates its own tables and makes no attempt to reuse any existing tables.它总是创建和填充自己的表,并且不尝试重用任何现有表。It is safer and simpler this way.这样更安全、更简单。
Watch the video tutorial to see it in action.观看视频教程,了解其实际效果。
Open MongoDB to SQL Migration开放MongoDB到SQL迁移
A MongoDB to SQL Migration task can be created from within the Tasks dialog, from the main toolbar:可以从“任务”对话框中的主工具栏创建“MongoDB到SQL迁移”任务:

Or from the Connection Tree’s context menu:或者从连接树的上下文菜单:

If a collection is already selected, Studio 3T will directly create and open a new export unit with a default configuration for that collection.如果已经选择了一个集合,Studio 3T将直接创建并打开一个带有该集合默认配置的新导出单元。
The interface consists of an Overview Tab, where the source connection, target connection, and export units can be configured.该界面由一个“概览选项卡”组成,可以在其中配置源连接、目标连接和导出单元。

An export unit defines one single export of a source collection.导出单元定义源集合的单个导出。
Each export unit can be viewed and edited within an Export Unit Tab, providing full access to its configuration as well as offering previews, log messages and more.可以在“导出单元”选项卡中查看和编辑每个导出单元,提供对其配置的完全访问,并提供预览、日志消息等功能。
Overview Tab“概述”选项卡
The Overview Tab provides a common source and target configuration for the export task. “概述”选项卡提供导出任务的通用源和目标配置。It also displays a list of single export units, each handling one source collection.它还显示单个导出单元的列表,每个导出单元处理一个源集合。

MongoDB source connectionMongoDB源连接
The common MongoDB connection for all export units, used for accessing source collections.所有导出单元的通用MongoDB连接,用于访问源集合。

SQL target server/directorySQL目标服务器/目录
The common target for all export units, which can be either a directory or a connection to a SQL server.所有导出单元的公共目标,可以是“目录”,也可以是“与SQL server的连接”。

If a target directory is set, a user may choose any target database type and each export unit specifies a relative SQL file path for the export.如果设置了目标目录,用户可以选择任何目标数据库类型,并且每个导出单元为导出指定一个相对的SQL文件路径。If a target server is set, the target database type is locked to the server type and each export unit specifies a database name for the export.如果设置了“目标服务器”,则目标数据库类型将“锁定”为服务器类型,并且每个导出单元为导出指定一个数据库名称。
Studio 3T currently supports MySQL, PostgreSQL, Oracle, Microsoft SQL Server, and IBM DB2.Studio 3T目前支持MySQL、PostgreSQL、Oracle、Microsoft SQL Server和IBM DB2。
Export Units出口单位
The table of all export units defined in this export task. 此导出任务中定义的所有导出单位的表。The properties shown for each export unit (row) are:每个导出单元(行)的属性如下:
Source database源数据库 –The name of the MongoDB source database.MongoDB源数据库的名称。Source collection源集合 –The name of the actual MongoDB source collection to export.要导出的实际MongoDB源集合的名称。Target file/database目标文件/数据库 –The name of the actual SQL file or SQL database to export to.要导出到的实际SQL文件或SQL数据库的名称。Mapping映射 –A short summary of the number of tables and columns that this unit exports.本单元导出的表和列数的简短摘要。
The tool bar provides actions to add, edit or remove export units. 工具栏提供了添加、编辑或删除导出单位的操作。These actions are also available through a context menu.这些操作也可以通过上下文菜单获得。
Additionally, collections of the selected source connection may be dragged from the connection tree and dropped into the export unit table to add new export units.此外,可以从连接树中拖动选定源连接的集合,并将其放入导出单元表中,以添加新的导出单元。

A user can easily change the order of export units using drag and drop.用户可以通过拖放轻松更改导出单位的顺序。
Single units can also be excluded from the export (see checkbox in first column of the Overview Tab).也可以从导出中排除单个单元(请参见“概述”选项卡第一列中的复选框)。
Adding units is only available with a valid source connection.添加单元仅在源连接有效的情况下可用。
Export Unit Tab“导出单位”选项卡
The Export Unit Tab shows the mapping configuration of the collection export to a SQL file or database. “导出单元”选项卡显示集合导出到SQL文件或数据库的映射配置。It also provides previews and live logs for exports.它还为导出提供预览和实时日志。
Initial Collection Scan首次集合扫描
The migration will automatically try to scan an export unit’s source collection as soon as a unit tab is opened the first time.第一次打开单元选项卡时,迁移将自动尝试扫描导出单元的源集合。
By default, the collection scan looks at the 50 newest documents 默认情况下,集合扫描会查看50个最新文档(which would be the last results returned by a simple find query).(这将是一个简单的find查询返回的最后一个结果)。
Studio 3T then creates default mappings based on the most frequently-occurring type found for a field.Studio 3T然后根据字段中最常见的类型创建默认映射。
If no type is found for a field, e.g. because all values seen are null or undefined, or if more than one field type has been detected, the type “Mixed” is chosen.如果找不到字段的类型,例如,因为看到的所有值都为null
或未定义,或者如果检测到多个字段类型,则选择“混合”类型。

Mappings映射

The Mappings button will open the Export Unit Tab which contains the Mapping Tree. “映射”按钮将打开包含映射树的导出单元选项卡。
The Mapping Tree shows how the fields of the MongoDB collection will be mapped to their corresponding SQL tables and columns. 映射树显示MongoDB集合的字段将如何映射到相应的SQL表和列。Its structure mostly follows the collection’s document schema.它的结构主要遵循集合的文档模式。
The first column (Field) contains a field name for each mapping, starting with the root node with the collection name and descending into embedded fields of its documents.第一列(字段)包含每个映射的字段名,从具有集合名称的根节点开始,向下到其文档的嵌入字段。Special case for arrays: A user may decide to map array elements by their type instead of their index.数组的特殊情况:用户可以决定按数组元素的类型而不是索引来映射数组元素。For this, “virtual” fields with type names (e.g. “Object”) are shown.为此,将显示具有类型名称的“虚拟”字段(例如“对象”)。New field mappings may be added and existing ones may be reordered or removed.可以添加新的字段映射,也可以重新排序或删除现有的字段映射。All field mappings can be edited directly within the Mapping Tree or with an edit dialog.所有字段映射都可以直接在映射树中进行编辑,也可以使用编辑对话框进行编辑。An alternative to removing a field mapping is to not include it.删除字段映射的另一种方法是不包含它。Mappings excluded from the export are shown in grey and their children, hidden (e.g. “lexicalEntries”).被排除在导出之外的映射以灰色显示,它们的子映射是隐藏的(例如“lexicalEntries”)。

The bulk of MongoDB to SQL mappings are done in the Mapping Type, Relationship Type, SQL Table, SQL Column and the (Map to) SQL Type columns:MongoDB到SQL的大部分映射是在“映射类型”、“关系类型”、“SQL表”、“SQL列”和“(映射到)SQL”类型列中完成的:

Mapping Type映射类型
The Mapping Type defines how a field’s data is mapped to columns in SQL.“映射类型”定义字段数据如何映射到SQL中的列。

Column exports a primitive field like a number, string, etc. to a single column in SQL.“列”将数字、字符串等基本字段导出到SQL中的单个列。Object Fields exports an object field (embedded document) including its children to SQL.“对象字段”将对象字段(嵌入文档)及其子对象导出到SQL。Array Elements To Rows exports an array (list) including its elements to rows in SQL.“数组元素到行”在SQL中将包含其元素的数组(列表)导出到行。Array Elements To Columns exports an array (list) including its elements to columns in SQL.“数组元素到列”在SQL中将包含其元素的数组(列表)导出到列。
Relationship Type关系类型
The Relationship Type defines in which SQL tables these columns will be stored, and how.“关系类型”定义这些列将存储在哪些SQL表中,以及如何存储。

Embed does not create a new table, but embeds new columns within the table of a parent mapping.“嵌入”不会创建新表,但会在父映射的表中嵌入新列。Table, PK <- FK stores columns in its own, new table, along with a foreign key column, referring to the primary key of the parent mapping’s table.“表,PK<-FK”在其自己的新表中存储列,以及一个外键列,该外键列引用父映射表的主键。Table, FK -> PK stores columns in its own, new table, along with a foreign key column on the table of its parent mapping, referring to the new table’s primary key.“表,FK->PK”将列存储在其自己的新表中,同时在其父映射的表上存储一个外键列,该外键列引用新表的主键。Table, No Link stores columns its own, new table, but does not create any links to or from the parent mapping’s table.“表中,无链接”将列存储在自己的新表中,但不会创建指向父映射表的链接,也不会创建来自父映射表的链接。The relationship is not preserved这种关系没有得到维护
Both Table, FK -> PK and Embed are incompatible with and therefore not available with the mapping type Array Elements To Rows.“表,FK->PK”和“嵌入”都与映射类型数组元素到行不兼容,因此不可用。
SQL TableSQL表
The SQL Table defines the target SQL table for a field mapping.“SQL表”定义字段映射的目标SQL表。

For the relationship type Embed, a table from a valid, direct parent mapping may be selected here.对于关系类型“嵌入”,可以在此处选择来自有效的直接父映射的表。For relationship types that introduce their own, new tables, a requested table name may be entered here.对于引入自己的新表的关系类型,可以在此处输入请求的表名。
SQL ColumnSQL列
The SQL Column defines the target SQL column for a field mapping.“SQL列”定义字段映射的目标SQL列。

This property is available for mappings of type Column and allows a user to request a specific SQL column name.此属性可用于“列”类型的映射,并允许用户请求特定的SQL列名。
Map to SQL Type
The (Map to) SQL Type defines which SQL type the data should be mapped to.“(映射到)SQL类型”定义数据应映射到的SQL类型。

This property is available for mappings of type Column and allows to request a specific SQL column type.此属性可用于“列”类型的映射,并允许请求特定的SQL列类型。It provides a recommended default.它提供了一个推荐的默认值。
While the Mapping Tree allows a user to enter any table or column name, it will always show the effective, resulting name in SQL (WYSIWYG).虽然映射树允许用户输入任何表或列名,但它将始终以SQL(WYSIWYG)的形式显示有效的结果名称。
SQL Table Primary KeysSQL表主键
All tables created by the mapping will at least get an automatic primary key column that increments an integer value starting at zero.通过映射创建的所有表至少将获得一个自动主键列,该列从零开始递增一个整数值。
The root mapping may set its child mapping for the field 根映射可以为要用作主键的字段_id
to be used as primary key (see context menu), as long as it maps to a single column of the main table._id
设置其子映射(请参见上下文菜单),只要它映射到主表的一列。
By default, an 默认情况下,如果满足上述要求且其MongoDB值不是字符串类型,则_id
field mapping will be used as provider for the primary key column if above requirements are met and its MongoDB value is not of a string type._id
字段映射将用作主键列的提供程序。
Reason: Some databases, like MySQL, restrict the use of text types for primary key constraints. Of course, users may still manually set to use their string 原因:一些数据库,比如MySQL,限制使用文本类型作为主键约束。当然,用户仍然可以手动设置为使用字符串id字段作为主键。_id
fields as primary keys.
Edit Mapping Dialog编辑映射对话框
For more complex mappings, users can use the convenient Edit Mapping Dialog, which can be used to both create or edit mappings:对于更复杂的映射,用户可以使用方便的“编辑映射”对话框,该对话框可用于创建或编辑映射:
It divides the configuration into three sections, similar to the Mapping Tree: Field, Mapping and Relationship. 它将配置分为三个部分,类似于映射树:字段、映射和关系。SQL specific properties are available within their specific editors.特定于SQL的属性在其特定的编辑器中可用。

The Field section supports configuring the field of a mapping with an auto-completion of detected field names and types.“字段”部分支持配置映射的字段,自动完成检测到的字段名和类型。

The option Automatically set type of known fields allows the dialog to update the field type when it detects a known field name while a user is typing.“自动设置已知字段的类型”选项允许对话框在用户键入时检测到已知字段名时更新字段类型。The option Warn when encountering a type mismatch enables logging of situations where a value during export does not match the type specified for the field mapping.“遇到类型不匹配时警告”选项允许记录导出期间的值与为字段映射指定的类型不匹配的情况。
The Mapping section supports configuring the mapping type, along with a little explanation of each available choice.“映射”部分支持配置映射类型,并对每个可用选项进行一些解释。

The mapping type Column allows to configure the name and type of its column in SQL.映射类型列允许在SQL中配置其列的名称和类型。The mapping type Array Elements To Rows allows to configure the name of its index column in SQL.通过将类型数组元素映射到行,可以在SQL中配置其索引列的名称。Both the mapping types Object Fields and Array Elements To Columns provide no further configuration options.映射类型对象字段和数组元素到列都不提供进一步的配置选项。
The Relationship section supports configuring the relationship type, again along with a little explanation of each available choice.Relationship(关系)部分支持配置关系类型,并对每个可用选项进行了简要说明。

The relationship type Embed allows to configure the parent mapping to inherit the table from.关系类型“嵌入”允许配置父映射以从中继承表。- The relationship types Table, PK <- FK, Table, FK -> PK and Table, No Link allow to set the name of their own table and its primary key column in SQL.
- In addition to that, the relationship types Table, PK <- FK and Table, FK -> PK also allow to configure the name of their foreign key columns in SQL.
Running a Monitored Export运行受监控的导出
The mapping configuration of an export unit can be run directly from the mapping tree tool bar:导出单元的映射配置可以直接从映射树工具栏运行:

Similar to an export started from the task bar, a background task will be queued for the its execution.与从任务栏启动的导出类似,后台任务将排队等待其执行。
Additionally, a monitored export provides a live monitor of the running export including log messages as well as detailed statistics:此外,受监控的导出提供了运行导出的实时监控,包括日志消息和详细统计信息:

Furthermore, a monitored export can be reverted after it has been finished or aborted using the tool bar action “Revert the monitored export …” next to “Run …”此外,受监控的导出可以在完成或中止后使用“运行”旁边的工具栏操作“还原受监控的导出”进行还原
New Mapping Dialog (Customize Default Mappings)新建映射对话框(自定义默认映射)
Each mapping configuration can be replaced with a new, customizable default mapping using the “New Mapping” action from the mapping tree tool bar (the blank document icon):可以使用映射树工具栏(空白文档图标)中的“新建映射”操作,将每个映射配置替换为新的、可自定义的默认映射:

It opens the New Mapping Dialog that allows the creation of a new default mapping for a collection, including an option to re-scan its document schema.

Export Previews导出预览
Previews use the first ten documents returned by a plain find query to the source collection, if available. Shown preview details are linked to the selection of the Mapping Tree.
SQL Table Preview
The tab SQL Table Preview in the lower part of the unit tab provides a visual live preview of SQL tables and columns that would result from the current configuration.

SQL Statement Preview
The tab SQL Statement Preview in the lower part of the unit tab provides a textual live preview of SQL statements written for an export with the current configuration.

The tool bar of the SQL statement preview provides some options to control the preview and its update behavior:
- Update statement preview manually updates the preview, in case live updates are turned off.
- Auto update statement preview can be used to turn live updates of the preview on or off, e.g. to improve performance.
- Show insert statements for one preview document only tells the preview to preview INSERT statements for one preview document only.
- Show insert statements for all preview documents tells the preview to preview INSERT statements for all available preview documents.
- Auto Scroll To Statements allows to choose what type of statements to scroll to by default (CREATE or INSERT).
- Document allows to choose for which of the preview documents the view should scroll to the INSERT statements by default.
There is also the Log Messages Preview in the lower part of the unit tab, which provides a preview of log messages to expect for an export with the current configuration.在unit选项卡的下半部分还有“日志消息预览”,它提供了使用当前配置导出日志消息的预览。
With some combinations of SQL clients and databases, results might not show up immediately. 对于SQL客户机和数据库的某些组合,结果可能不会立即显示。A reconnect to the database should help in those cases.在这些情况下,重新连接数据库应该会有所帮助。
Export Log Files导出日志文件
With Studio 3T 2020.7, MongoDB to SQL Migration now also writes warnings and errors to a log file.在Studio 3T 2020.7中,MongoDB到SQL的迁移现在也会将警告和错误写入日志文件。
These log files are available for both live migrations and migrations saved as tasks.这些日志文件可用于实时迁移和保存为任务的迁移。
To view and save these log files locally, right-click on any MongoDB to SQL Migration export unit in the Operations window.要在本地查看和保存这些日志文件,请右键单击操作窗口中的任何MongoDB到SQL迁移导出单元。

Video tutorial视频教程
►https://www.youtube-nocookie.com/embed/yt8TdNPDvfY
Automate MongoDB to SQL Migrations自动化MongoDB到SQL的迁移
Interested in automating MongoDB to SQL migrations? Help us shape the future of job automation for MongoDB.有兴趣自动化MongoDB到SQL的迁移吗?帮助我们塑造MongoDB工作自动化的未来。
In the meantime, you can already run and schedule MongoDB to SQL migrations on your local machine using Studio 3T’s Task Scheduler tool.同时,您已经可以使用Studio 3T的任务调度器工具在本地机器上运行并计划MongoDB到SQL的迁移。
What’s next?接下来呢?
Download Studio 3T to try out the bi-directional SQL Migration feature下载Studio 3T以试用双向SQL迁移功能Check out the Export Wizard for more info on supported export formats有关支持的导出格式的详细信息,请查看导出向导Get the whitepaper, SQL Migration: MongoDB and SQL Coexistence, Made Easy获取白皮书《SQL迁移:MongoDB和SQL共存》,使之变得容易