You can use Data Transmission Service (DTS) to migrate data from MongoDB to an AnalyticDB for PostgreSQL instance. This topic describes how to migrate data from an ApsaraDB for MongoDB replica set instance.
Prerequisites
Create a destination AnalyticDB for PostgreSQL instance. The storage space of the destination instance must be larger than the storage space used by the source ApsaraDB for MongoDB instance. For more information, see Create an instance.
NoteThe storage space of the destination instance should be at least 10% larger than the storage space used by the source database.
In the destination AnalyticDB for PostgreSQL instance, create a database, a schema, and a table with a primary key for the migrated data. For more information, see SQL syntax.
ImportantEnsure that the data types in the destination table are compatible with the source MongoDB data. For example, if the
_idfield in MongoDB is of the ObjectId data type, the corresponding data type in the AnalyticDB for PostgreSQL instance must be varchar.The column names in the destination table of the AnalyticDB for PostgreSQL instance cannot be _id or _value.
If the source ApsaraDB for MongoDB instance is a sharded cluster, you must obtain endpoints for all shard nodes. The database accounts and passwords for each shard must be the same. For more information, see Apply for a shard endpoint.
Notes
Type | Description |
Source database limits |
|
Other limits |
|
Billing
Migration type | Link configuration fee | Data transfer cost |
Full data migration | Free of charge. | This example is free of charge. If you set Access Method for the destination database to Public IP Address, you are charged for data transfer over the Internet. |
Incremental data migration | Charged. For more information, see Billing overview. |
Migration types
Migration type | Description |
Full migration | Migrates all historical data of the migration objects from the source ApsaraDB for MongoDB instance to the destination AnalyticDB for PostgreSQL instance. |
Incremental migration | In addition to full migration, this migrates incremental updates from the source ApsaraDB for MongoDB instance to the destination AnalyticDB for PostgreSQL instance. Note
|
Required database account permissions
Database | Full migration | Incremental migration | Account creation and authorization method |
Source ApsaraDB for MongoDB | Read permissions on the databases to be migrated. | Read permissions on the databases to be migrated, the admin database, and the local database. | |
Destination AnalyticDB for PostgreSQL | Read and write permissions on the destination database. | Create and manage users and Manage user permissions. Note You can use the initial account or an account with the RDS_SUPERUSER permission. | |
Procedure
-
Navigate to the migration task list page for the destination region using one of the following methods.
From the DTS console
-
Log on to the Data Transmission Service (DTS) console.
-
In the navigation pane on the left, click Data Migration.
-
In the upper-left corner of the page, select the region where the migration instance is located.
From the DMS console
NoteThe actual operations may vary based on the mode and layout of the DMS console. For more information, see Simple mode console and Customize the layout and style of the DMS console.
-
Log on to the Data Management (DMS) console.
-
In the top menu bar, choose .
-
To the right of Data Migration Tasks, select the region where the migration instance is located.
-
-
Click Create Task to navigate to the task configuration page.
Configure the source and destination databases.
Category
Configuration
Description
None
Task Name
DTS automatically generates a task name. We recommend that you specify a descriptive name for easy identification. The name does not need to be unique.
Source Database
Select Existing Connection
-
To use a database instance that has been added to the system (created or saved), select the desired database instance from the drop-down list. The database information below will be automatically configured.
NoteIn the DMS console, this parameter is named Select a DMS database instance..
-
If you have not registered the database instance with the system, or do not need to use a registered instance, manually configure the database information below.
Database Type
The type of the source database. Select MongoDB.
Access Method
Select Alibaba Cloud Instance.
Instance Region
Select the region where the source ApsaraDB for MongoDB instance resides.
Replicate Data Across Alibaba Cloud Accounts
In this example, a database instance under the current Alibaba Cloud account is used. Select No.
Architecture
In this example, Replica Set is selected.
NoteIf your source ApsaraDB for MongoDB is a Sharded Cluster instance, you must also specify Shard account and Shard password.
Migration Method
Select an incremental data migration method based on your situation.
Oplog (recommended):
Available if Oplog is enabled on the source database.
NoteOplog is enabled by default for self-managed MongoDB and ApsaraDB for MongoDB. Using Oplog results in lower latency for incremental migration tasks (faster log retrieval), so we recommend selecting Oplog.
ChangeStream: Available if Change Streams (Change Streams) are enabled on the source database.
NoteIf the source database is Amazon DocumentDB (non-elastic cluster), you can only select ChangeStream.
If Architecture is set to Sharded Cluster, you do not need to enter Shard account and Shard password.
Instance ID
Select the instance ID of the source ApsaraDB for MongoDB instance.
Authentication Database
Enter the name of the database to which the database account of the source ApsaraDB for MongoDB instance belongs. The default value is admin.
Database Account
Enter the database account of the source ApsaraDB for MongoDB instance. For information about the required permissions, see Required database account permissions.
Database Password
Enter the password for the specified database account.
Encryption
DTS supports three connection types: Non-encrypted, SSL-encrypted, and Mongo Atlas SSL. The options available for the Encryption parameter are determined by the values selected for the Access Method and Architecture parameters. The options displayed in the DTS console prevail.
NoteMongoDB databases where the Architecture is Sharded Cluster and the Migration Method is Oplog do not support SSL-encrypted.
If the source database is a self-managed MongoDB database that uses the Replica Set, the Access Method is not set to Alibaba Cloud Instance, and you have selected SSL-encrypted, you can also upload a certification authority (CA) certificate to verify the connection to the source database.
Destination Database
Select Existing Connection
-
To use a database instance that has been added to the system (created or saved), select the desired database instance from the drop-down list. The database information below will be automatically configured.
NoteIn the DMS console, this parameter is named Select a DMS database instance..
-
If you have not registered the database instance with the system, or do not need to use a registered instance, manually configure the database information below.
Database Type
Select AnalyticDB for PostgreSQL.
Access Method
Select Alibaba Cloud Instance.
Instance Region
Select the region where the destination AnalyticDB for PostgreSQL instance resides.
Instance ID
Select the instance ID of the destination AnalyticDB for PostgreSQL instance.
Database Name
Enter the name of the database in the destination AnalyticDB for PostgreSQL instance that will receive the migrated objects.
Database Account
Enter the database account of the destination AnalyticDB for PostgreSQL instance. For information about the required permissions, see Required database account permissions.
Database Password
Enter the password for the specified database account.
-
-
After you complete the configuration, click Test Connectivity and Proceed at the bottom of the page.
Note-
Ensure that the IP address segment of the DTS service is automatically or manually added to the security settings of the source and destination databases to allow access from DTS servers. For more information, see Add DTS server IP addresses to a whitelist.
-
If the source or destination database is a self-managed database (the Access Method is not Alibaba Cloud Instance), you must also click Test Connectivity in the CIDR Blocks of DTS Servers dialog box that appears.
-
Configure the task objects.
On the Configure Objects page, configure the objects that you want to migrate.
Configuration
Description
Migration Types
-
If you only need to perform a full migration, select Full Data Migration.
-
To perform a migration with no downtime, select both Full Data Migration and Incremental Data Migration.
NoteIf you do not select Incremental Data Migration, do not write new data to the source instance during data migration to ensure data consistency.
DDL and DML Operations to Be Synchronized
Select the operations for incremental migration at the instance level.
NoteTo select operations for incremental migration at the collection level, right-click a migration object in the Selected Objects list and select the checkboxes in the dialog box.
Processing Mode of Conflicting Tables
-
Precheck and Report Errors: Checks whether collections with the same names exist in the destination database. If no collections with the same names exist, the precheck is passed. If collections with the same names exist, an error is reported during the precheck, and the data migration task does not start.
NoteIf a collection in the destination database has the same name but cannot be easily deleted or renamed, you can change the name of the collection in the destination database. For more information, see Object name mapping.
-
Ignore Errors and Proceed: Skips the check for collections with the same names.
WarningSelecting Ignore Errors and Proceed may cause data inconsistency and business risks. For example:
-
If a record in the destination database has the same primary key value as a record in the source database, the record in the destination database is kept. The record from the source database is not migrated to the destination database.
-
Data initialization may fail, only some data may be migrated, or the migration may fail.
-
Source Objects
In the Source Objects box, click the objects to migrate, and then click
to move them to the Selected Objects box.NoteYou can select objects to migrate at the collection level.
Selected Objects
Edit database name mapping.
Right-click the database that contains the collection to be migrated in the Selected Objects section.

Change Database Name to the name of the schema that will receive the data in the destination AnalyticDB for PostgreSQL instance.

Optional: In the Select DDL and DML Operations to Be Synchronized area, select the operations required for incremental migration.

Click OK.
Edit table name mapping.
Right-click the collection to be migrated in the Selected Objects section.

Change Table Name to the name of the table that will receive the data in the destination AnalyticDB for PostgreSQL instance.

Optional: Set filter conditions. For more information, see Set filter conditions.

Optional: In the Select DDL and DML Operations to Be Synchronized section, select the operations for incremental migration.

Configure the MongoDB fields to migrate.
By default, DTS maps the data of the collection to be migrated. You can configure an expression in the Assignment column. Check whether the expression meets your requirements and configure parameters such as Column Name, Type, Length, and Precision.
ImportantThe primary key column of the destination table must be assigned the value
bson_value("_id").When you configure the
bson_value()expression, you must specify the path down to the lowest-level subfield. Otherwise, data loss or task failure may occur.
In the Assignment column, check the
bson_value()expression to identify the corresponding MongoDB field.The field within
""is the field name in MongoDB. For example, if the expression isbson_value("age"), the row corresponds to theagefield in MongoDB.Optional: Delete fields that you do not need to migrate.
NoteFor fields that you do not need to migrate, you can click
at the end of the row.Configure the fields to be migrated.
Perform the following operations based on whether the
bson_value()expression meets your requirements.Fields with correct expressions
Enter the Column Name.
NoteThis is the name of the column in the destination AnalyticDB for PostgreSQL instance that will receive the data.
Select the Type of the column data.
ImportantEnsure that the data types in the destination table are compatible with the source MongoDB data.
Optional: Configure the Length and Precision for the column data.
Repeat the preceding steps to map all relevant fields.
Fields with incorrect expressions
NoteFor example, fields with a hierarchical structure (parent-child).
In the Actions column, click
for the entry.Click + Add Column.

Configure Column Name, Type, Length, and Precision.
In the text box under Assignment, enter the
bson_value()expression. For more information, see Assignment Configuration Examples.Repeat the preceding steps to map all relevant fields.
Click OK.
-
Click Next: Advanced Settings to configure advanced parameters.
Configuration
Description
Dedicated Cluster for Task Scheduling
By default, DTS schedules tasks on a shared cluster. You do not need to select one. If you want more stable tasks, you can purchase a dedicated cluster to run DTS migration tasks.
Retry Time for Failed Connections
After the migration task starts, if the connection to the source or destination database fails, DTS reports an error and immediately begins to retry the connection. The default retry duration is 720 minutes. You can customize the retry time to a value from 10 to 1440 minutes. We recommend that you set the duration to more than 30 minutes. If DTS reconnects to the source and destination databases within the specified duration, the migration task automatically resumes. Otherwise, the task fails.
Note-
For multiple DTS instances that share the same source or destination, the network retry time is determined by the setting of the last created task.
-
Because you are charged for the task during the connection retry period, we recommend that you customize the retry time based on your business needs, or release the DTS instance as soon as possible after the source and destination database instances are released.
Retry Time for Other Issues
After the migration task starts, if a non-connectivity issue, such as a DDL or DML execution exception, occurs in the source or destination database, DTS reports an error and immediately begins to retry the operation. The default retry duration is 10 minutes. You can customize the retry time to a value from 1 to 1440 minutes. We recommend that you set the duration to more than 10 minutes. If the related operations succeed within the specified retry duration, the migration task automatically resumes. Otherwise, the task fails.
ImportantThe value of Retry Time for Other Issues must be less than the value of Retry Time for Failed Connections.
Enable Throttling for Full Data Migration
During full migration, DTS consumes read and write resources on the source and destination databases, which may increase the database load. If required, you can enable throttling for the full migration task. You can set Queries per second (QPS) to the source database, RPS of Full Data Migration, and Data migration speed for full migration (MB/s) to reduce the load on the destination database.
Note-
This configuration item is available only if you select Full Data Migration for Migration Types.
-
You can also adjust the full migration speed after the migration instance is running.
Only one data type for primary key _id in a table of the data to be synchronized
Indicates whether the data type of the primary key
_idis unique within the same collection in the data to be migrated.ImportantSelect as needed. Otherwise, data loss may occur.
This configuration is available only if Migration Types includes Full Data Migration.
Yes: Unique. During full migration, DTS does not scan the data types of primary keys in the source database. For each collection, DTS migrates data corresponding to only one primary key data type.
No: Not unique. During full migration, DTS scans the data types of primary keys in the source database and migrates all data.
Enable Throttling for Incremental Data Migration
If required, you can also choose to set speed limits for the incremental migration task. You can set RPS of Incremental Data Migration and Data migration speed for incremental migration (MB/s) to reduce the load on the destination database.
Note-
This configuration item is available only if you select Incremental Data Migration for Migration Types.
-
You can also adjust the incremental migration speed after the migration instance is running.
Environment Tag
You can select an environment tag to identify the instance. In this example, no selection is required.
Configure ETL
Choose whether to enable the extract, transform, and load (ETL) feature. For more information, see What is ETL? Valid values:
-
Yes: Enables the ETL feature. Enter data processing statements in the code editor. For more information, see Configure ETL in a data migration or data synchronization task.
-
No: Disables the ETL feature.
Monitoring and Alerting
Select whether to set alerts and receive alert notifications based on your business needs.
-
No: Does not set an alert.
-
Yes: Configure alerts by setting an alert threshold and an alert notifications. If a migration fails or the latency exceeds the threshold, the system sends an alert notification.
-
-
Save the task and run a precheck.
-
To view the parameters for configuring this instance when you call the API operation, move the pointer over the Next: Save Task Settings and Precheck button and click Preview OpenAPI parameters in the bubble that appears.
-
If you do not need to view or have finished viewing the API parameters, click Next: Save Task Settings and Precheck at the bottom of the page.
Note-
Before the migration task starts, DTS performs a precheck. The task starts only after it passes the precheck.
-
If the precheck fails, click View Details next to the failed check item, fix the issue based on the prompt, and then run the precheck again.
-
If a warning is reported during the precheck:
-
For check items that cannot be ignored, click View Details next to the failed item, fix the issue based on the prompt, and then run the precheck again.
-
For check items that can be ignored, you can click Confirm Alert Details, Ignore, OK, and Precheck Again to skip the alert item and run the precheck again. If you choose to ignore a warning, it may cause issues such as data inconsistency and pose risks to your business.
-
-
Purchase the instance.
-
When the Success Rate is 100%, click Next: Purchase Instance.
-
On the Purchase page, select the link specification for the data migration instance. For more information, see the following table.
Category
Parameter
Description
New Instance Class
Resource Group Settings
Select the resource group to which the instance belongs. The default value is default resource group. For more information, see What is Resource Management?
Instance Class
DTS provides migration specifications with different performance levels. The link specification affects the migration speed. You can select a specification based on your business scenario. For more information, see Data migration link specifications.
-
After the configuration is complete, read and select Data Transmission Service (Pay-as-you-go) Service Terms.
-
Click Buy and Start. In the OK dialog box that appears, click OK.
You can view the progress of the migration task on the Data Migration Tasks list page.
Note-
If the migration task does not include incremental migration, it stops automatically after the full migration is complete. After the task stops, its Status changes to Completed.
-
If the migration task includes incremental migration, it does not stop automatically. The incremental migration task continues to run. While the incremental migration task is running, the Status of the task is Running.
-
-
Value configuration example
Source MongoDB data structure
{
"_id":"62cd344c85c1ea6a2a9f****",
"person":{
"name":"neo",
"age":26,
"sex":"male"
}
}Destination AnalyticDB for PostgreSQL table schema
Column Name | Type |
mongo_id | varchar Note Primary key column. |
person_name | varchar |
person_age | decimal |
Add Column configuration
You must correctly configure the bson_value() expression based on the data hierarchy. Otherwise, data loss or a task failure may occur. For example, if you configure the expression as bson_value("person"), DTS cannot write incremental changes for the child fields (`name`, `age`, and `sex`) of the person field from the source to the destination.
Column Name | Type | Assignment |
mongo_id | STRING | bson_value("_id") |
person_name | STRING | bson_value("person","name") |
person_age | DECIMAL | bson_value("person","age") |