Azure Cosmos DB for Mongo Series: Part 1: Setting up of CosmosDB

In the past weeks, we’ve received several requests to migrate data from CosmosDB to MongoDB. I’ve been working on experimenting with the process, and this five-part series on how to migrate data to MongoDB Atlas from CosmosDB is the result of documenting my approach. As you may be aware, both Azure CosmosDB and MongoDB Atlas are document model databases that are offered as managed services, and they each have their own pros and cons.

Azure Cosmos DB is a fully-managed, globally-distributed, multi-model database, and provides support for various data models such as SQL, Cassandra, Gremlin, Table and MongoDB.

MongoDB Atlas is a fully-managed cloud database developed by the same people that built MongoDB. It can handle all the complexities of deploying, managing, and healing your deployments on the cloud service provider of your choice (AWS, Azure, and GCP).

To understand how to address data and application migration to MongoDB Atlas, I have broken down the series thus:

Part 1 : Setting up Azure CosmosDB with Mongo API
Part 2 : Simple migration scenarios (homogeneous and heterogeneous clouds) leveraging native tools
Part 3 : Complex migration scenarios (things to consider and plan)
Part 4 : CDC options for migrating ongoing changes (connected and disconnected scenarios)
Part 5 : Migration using Spark Connectors (Cosmos Spark Connector and MongoDB Spark Conne ctor)

For the purpose of the blog, I’ve considered CosmosDB with MongoDB API as the source for migration.

In this first post of the series, I would like to talk about how to set up CosmosDB for MongoDB API with sample documents, and then explore how .NET Mongo driver can be used to connecte and access the same.

In the second half of this blog, we shall export the data to Atlas, and seamlessly migrate the application with a simple switchover of connection string.

Creating Azure Cosmos DB for MongoDB

Select Azure Cosmos DB for MongoDB API from the list of APIs, and select the the required version.

Network Connectivity

For the purpose of the blog, I have assumed that the connection is allowed only for specific IPs. Ideally, VNet peering is configured in this step.

Once done, follow the steps in the wizard to create the CosmosDB.

Test Data Population

Use the mongoimport tool to get documents into the Cosmos collection. Add sample data – for example: docs.json – in the git path mentioned below:

https://github.com/PeerIslands/cosmos-mongo-example.git

mongoimport -h <<yourcoscosmosdbname>>.mongo.cosmos.azure.com:10255 \-d test -c example -u <<yourusername>> \-p <<yourpassword>>\–ssl –jsonArray –file example-docs.json –writeConcern=”{w:0}”

Cosmos Data Explorer

Once the data is imported, use the Data Explorer to view the data:

.NET Console App

Retrieve the Connection String from the Azure portal, and configure it in the .NET console. Retrieve a sample code from https://github.com/PeerIslands/cosmos-mongo-example.git

Update the Connection String, and run the console app.

Sample Console App

We are now connected, and can perform basic CRUD operations.

Read Part 2 of the series.

Rajesh Vinayagam