Azure Cosmos DB for MongoDB: CosmosDB Setup — Part 1

Rajesh Vinayagam
3 min readMar 13, 2021

Azure Cosmos DB is a fully managed and globally distributed, Multi-model databases with the support for various data models like SQL, Cassandra, Gremlin, Table and MongoDB.

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

I decided to write this blog-post series on “How to Atlas from CosmosDB”, to throw some light and ideas on data and application migration.

How to Atlas from CosmosDB Series
Part 1 : Set up of Azure CosmosDB with Mongo API
Part 2 : Simple Migration Scenarios( homogeneous & heterogeneous clouds) leveraging native Tools
Part 3 : Complex Migration Scenarios ( things to consider and plan )
Part 4 : CDC Options for migrating the ongoing changes ( connected and disconnected scenarios )
Part 5 : Migration using Spark Connectors ( Cosmos Spark Connector & MongoDB Spark Connector)

PS: CosmosDB with Mongo API is considered as a source for migration.

In this article we will quickly explore how to set up CosmosDB for Mongo API with few collections & documents, and later use .NET Mongo driver for connecting and accessing the same.

In the second part of the article we will have this data exported to Atlas and see how we can seamlessly migrate the application with a simple switchover of connection string.

Create Azure Cosmos DB for Mongo

API & Version Selection

Select Azure Cosmos DB for MongoDB API from the list of API and have the required version selected.

Network Connectivity

For this demo we will have the connection allowed for only specific IP’s, ideally VNet peering should be configured in this step.

Follow the rest of the steps in the wizard to have the CosmosDB created.

Test Data Population

Import Test Data

Use the mongoimport tool to get some documents into the Cosmos collection. Have some sample data(example-docs.json) in the below git path

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 see the data as shown below.

.NET Console App

Have written a simple console app in .NET to connect to Azure CosmosDB for Mongo API using the Mongo driver for .NET.

Retrieve the connection string from the azure portal and configure in the .NET Console. The sample code can be pulled from the below location.

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

Update the Connection string and run the below console app.

Sample Console App

We are now able to connect and do some basic CRUD operations. Lets continue to Part 2 of the series.

--

--