Migrating collections from MongoDB to Yandex StoreDoc
To migrate collections stored in a third-party MongoDB cluster to a Yandex StoreDoc cluster, you need to transfer the data, write-lock the old database, and transfer the load to a Yandex Cloud cluster.
Required paid resources
The support cost for this solution includes:
- Yandex StoreDoc target cluster fee: use of computing resources allocated to hosts and disk space (see Yandex StoreDoc pricing).
- Per-transfer fee: use of computing resources and number of transferred data rows (see Data Transfer pricing).
- Fee for public IP addresses if public access is enabled for cluster hosts (see Virtual Private Cloud pricing).
Getting started
Make sure that you can connect to the source cluster hosts from the internet.
Migrating data using Yandex Data Transfer
-
Create a source endpoint with the following parameters:
-
Database type:
MongoDB. -
Endpoint parameters → Connection settings:
Custom installation.Configure the source cluster connection settings.
Note
Transferring of
Time Seriescollections is not supported, so you should exclude such collections in the endpoint settings. -
-
Create a target endpoint with the following parameters:
-
Database type:
MongoDB. -
Endpoint parameters → Connection settings:
Yandex StoreDoc cluster.Specify the ID of the target cluster.
-
-
Create a Snapshot and increment-type transfer and configure it to use the previously created endpoints.
To make large collections (over 1 GB) copy more quickly, enable parallel copy in the transfer settings. Specify two or more workers. The collection will be split into the specified number of parts that will be copied concurrently.
For parallel copy to work, the
_idfield data type must be the same for all documents in the same collection. If a transfer discovers a type mismatch, the collection will not be partitioned but transferred in a single thread instead. If needed, remove documents with mismatched data types from the collection before starting a transfer.Note
If a document with a different data type is added to a collection after a transfer starts, the transfer will move it at the replication stage after the parallel copy operation is completed. However, when re-activated, the transfer will not be able to partition a collection because the
_idfield type requirement will not be met for some of the documents in the collection. -
Wait for the transfer status to change to Replicating.
-
Switch the source cluster to "read-only" mode and transfer the load to the target cluster.
-
On the transfer monitoring page, wait for the Maximum data transfer delay metric to decrease to zero. This means that all changes that occurred in the source cluster after data copying was completed are transferred to the target cluster.
-
Deactivate the transfer and wait for its status to change to Stopped.
For more information about transfer statuses, see Transfer lifecycle.
Example of migrating a collection
- Set up the source cluster.
- Create and configure a target cluster.
- Set up endpoints and a transfer.
- Transfer data.
- Delete the resources you created.
Set up the source cluster
-
Connect to the
mongoshost of the MongoDB source cluster usingmongosh. -
Create a
db1. -
Create a user with the
db1owner permissions and log in with that user's credentials:use admin db.createUser({ user: "user1", pwd: passwordPrompt(), roles: [ { role: "dbOwner", db: "db1" } ] }) db.auth("user1") -
Fill in
collection1indb1with random values. 200,000 entries will be added to the collection:use db1 function randomName() { // Base 36 uses letters and digits to represent a number: return (Math.random()+1).toString(36).substring(2); } var day = 1000 * 60 * 60 * 24; function randomDate () { return new Date(Date.now() - (Math.floor(Math.random() * day))); } for (var i = 1; i <= 200000; ++i) { db.collection1.insertOne({ name: randomName(), creationDate: randomDate(), uid: i }); }
Create and configure a target cluster
- Create a Yandex StoreDoc cluster with any suitable configuration.
- Create a database named
db1. - Create a user named
user_transferwith thereadWriterole for the new database.
- Create a Yandex StoreDoc cluster with any suitable configuration. There must be at least two hosts in the cluster.
- Enable sharding.
- Create a database named
db1. - Create a user named
user_transferwith thereadWriterole for the created database and themdbShardingManagerrole for theadminservice base. - Following these steps, create an empty sharded collection named
collection1indb1and configure it.
Set up endpoints and a transfer
- Create an endpoint for the source cluster:
- Database type:
MongoDB. - Connection settings:
Custom installation.- CA certificate: Upload the certificate file if a connection to the source cluster requires encryption.
- Host list: Specify the FQDN of the source cluster hosts.
- Port: Specify the port for connecting to the hosts.
- Authentication source:
db1. - User:
user1. - Password: Enter the
user1password.
- Database type:
- Create an endpoint for the target cluster:
- Database type:
MongoDB. - Connection settings:
MDB cluster.- Specify the ID of the target cluster.
- User:
user_transfer. - Password: Enter the
user_transferpassword. - Cleanup policy:
Drop.
- Database type:
- Create a transfer:
- Transfer type: Snapshot and increment.
- Source: Select the created endpoint for the source cluster.
- Target: Select the created endpoint for the target cluster.
- Create an endpoint for the source cluster:
- Database type:
MongoDB. - Connection settings:
Custom installation.- CA certificate: Upload the certificate file if a connection to the source cluster requires encryption.
- Host list: Specify the FQDN of the source cluster hosts.
- Port: Specify the port for connecting to the hosts.
- Authentication source:
db1. - User:
user1. - Password: Enter the
user1password.
- Database type:
- Create an endpoint for the target cluster:
- Database type:
MongoDB. - Connection settings:
MDB cluster.- Specify the ID of the target cluster.
- User:
user_transfer. - Password: Enter the
user_transferpassword. - Cleanup policy:
Don't cleanuporTruncate.
- Database type:
- Create a transfer:
- Transfer type: Snapshot and increment.
- Source: Select the created endpoint for the source cluster.
- Target: Select the created endpoint for the target cluster.
Transfer the data
-
Activate the created transfer.
-
Wait for the transfer status to change to Replicating.
-
Switch the source cluster to
read-only
mode and transfer the load to the target cluster. -
On the transfer monitoring page, wait for the Maximum data transfer delay metric to decrease to zero. This means that all changes that occurred in the source cluster after data copying was completed are transferred to the target cluster.
-
Connect to the target cluster.
-
Make sure
collection1has been transferred and contains 200,000 documents as in the source cluster:use db1 db.collection1.countDocuments()200000
-
Activate the created transfer.
-
Wait for the transfer status to change to Replicating.
-
Switch the source cluster to
read-only
mode and transfer the load to the target cluster. -
On the transfer monitoring page, wait for the Maximum data transfer delay metric to decrease to zero. This means that all changes that occurred in the source cluster after data copying was completed are transferred to the target cluster.
-
Connect to the target cluster.
-
Make sure
collection1has been transferred, contains 200,000 documents as in the source cluster, and the documents are distributed across shards:use db1 db.collection1.countDocuments()200000db.collection1.getShardDistribution()Shard rs01 at rs01/<host_1> { data: '7.05MiB', docs: 99752, chunks: 2, 'estimated data per chunk': '3.52MiB', 'estimated docs per chunk': 49876 } --- Shard rs02 at rs02/<host_2> { data: '7.08MiB', docs: 100248, chunks: 2, 'estimated data per chunk': '3.54MiB', 'estimated docs per chunk': 50124 } --- ...
Delete the resources you created
Some resources are not free of charge. To avoid paying for them, delete the resources you no longer need:
-
Deactivate the transfer and wait for its status to change to Stopped.
To learn more about the transfer lifecycle, see this Yandex Data Transfer guide.
-
Delete the stopped transfer.