DEV Community

Cover image for Data Migration from MongoDB to Azure Cosmos DB
Paschal Kenechukwu Oruche
Paschal Kenechukwu Oruche

Posted on

Data Migration from MongoDB to Azure Cosmos DB

In today's piece, I'll be exposing to you, the process that was implemented to migrate data from a MongoDB Cloud cluster to Azure CosmosDB for MongoDB. Although, before we begin to churn out the detailed steps and commands, let's expand a bit on the major elements of this write up. *What really is Azure CosmosDB? *

Microsoft Azure Cosmos DB

Curled from Microsoft Learn, Azure Cosmos DB is a fully managed NoSQL and relational database for modern app development. Azure Cosmos DB offers single-digit millisecond response times, automatic and instant scalability, along with guaranteed speed at any scale. To the best of my understanding, AZ Cosmos DB features:

  • NoSQL
  • MongoDB
  • Apache
  • Cassandra
  • Apache Gremlin
  • Table
  • PostgreSQL

There are a couple of other resources, we can look up to gain further insights on these Database technologies.

What is MongoDB?
MongoDB is a popular open-source, NoSQL database that falls under the category of document-oriented databases. It was developed by MongoDB Inc. and is designed to handle large volumes of data, making it suitable for modern applications that require flexible and scalable storage solutions.

Mongo DB

*Let's get started, shall we!
*

Pre-requisite:

  1. MongoDB Database up and running

  2. CosmosDB Database up and running
 Requirements:
  3. MongoDB connection string

  4. CosmosDB for MongoDB connection string
   Step 1

Set up Azure Cosmos DB: Create an Azure Cosmos DB account with the MongoDB API in your Azure subscription.
Note down the connection string for your Azure Cosmos DB account.

Step 2

Export Data from MongoDB Atlas: Export the data from your MongoDB Atlas cluster using the available tools and methods.
MongoDB provides several options for exporting data, including the mongodump command-line tool, mongoexport utility, or MongoDB Compass. mongodump command was used.



Step 3

Open powershell and run the mongodump command, mongodb will export the data from the database and store it in local machine. Command below:

mongodump --uri=<Atlas_Connection_String> --out=<Output_Directory>

Note: Remember to replace with the connection string of MongoDB Atlas cluster, and replace with the path where you want to save the exported data

Step 4

After exporting data from MongoDB to local machine, migrate the data to Azure CosmosDB for MongoDB using the mongorestore

Step 5

Prepare the backup: Ensure that you have a backup of your MongoDB data created using mongodump. The backup should be in a directory containing the BSON files and metadata.

Step 6

Run mongorestore
Two methods:
A)
mongorestore --host <hostname> --port <port> --username <username> --password <password> --authenticationDatabase <authDatabase> <backup_directory_path>

B)
mongorestore --uri <connection_uri> <backup_directory_path> -d mydb

Using method B, run the command below replacing the placeholders:

mongorestore --uri "mongodb://username:password@localhost:27017/" /path/to/backup -d mydb

Step 7

Wait for mongorestore to complete the data restoration process. The time required will be depending on the size of the data and the server’s performance.

Step 8

Verify the data by connecting to the Azure CosmosDB for MongoDB and perform queries or checks on the restored database

Step 9

Now, connect your application to use the Azure CosmosDB for MongoDB connection string
.

🫵

Top comments (0)