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? *
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:
- Apache Gremlin
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.
*Let's get started, shall we!
- MongoDB Database up and running
- CosmosDB Database up and running Requirements:
- MongoDB connection string
- 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.
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.
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
After exporting data from MongoDB to local machine, migrate the data to Azure CosmosDB for MongoDB using the mongorestore
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.
mongorestore --host <hostname> --port <port> --username <username> --password <password> --authenticationDatabase <authDatabase> <backup_directory_path>
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
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.
Verify the data by connecting to the Azure CosmosDB for MongoDB and perform queries or checks on the restored database
Now, connect your application to use the Azure CosmosDB for MongoDB connection string .