Azure

6 Mins Read

Exploring Azure Cosmos DB with MongoDB API: The Best of Both Worlds

Voiced by Amazon Polly

In today’s data-driven world, businesses need scalable, high-performance databases that can adapt to their needs. Azure Cosmos DB, Microsoft’s globally distributed, multi-model database, meets these demands with unparalleled performance and flexibility. And with the MongoDB API, it goes a step further by enabling developers to harness the power of Cosmos DB while using familiar MongoDB tools, libraries, and queries.

This blog dives into what Azure Cosmos DB with MongoDB API is, how it works, and why it’s an excellent choice for developers and businesses.

Become an Azure Expert in Just 2 Months with Industry-Certified Trainers

  • Career-Boosting Skills
  • Hands-on Labs
  • Flexible Learning
Enroll Now

Introduction to Azure Cosmos DB with MongoDB API?

Azure Cosmos DB with MongoDB API is a feature that allows developers to interact with Cosmos DB using the MongoDB protocol. Essentially, Cosmos DB acts as a managed backend for MongoDB workloads, enabling you to use MongoDB drivers, tools, and query syntax while benefiting from Cosmos DB’s global distribution, elastic scalability, and enterprise-grade features.

With the MongoDB API, you can seamlessly migrate existing MongoDB workloads to Azure Cosmos DB without rewriting your application code. This makes it ideal for organizations looking to modernize their database infrastructure with minimal disruption.

Key Features and Benefits

1. Familiar MongoDB Ecosystem
Developers can use the same MongoDB drivers, libraries, and tools they’re already accustomed to, such as mongoimport, mongoexport, and MongoDB Compass. This reduces the learning curve and allows for quick adoption.

2. Global Distribution
Cosmos DB automatically replicates your data across multiple regions, ensuring low latency reads and writes for globally distributed applications. With MongoDB API, you can leverage this feature while using MongoDB’s familiar syntax.

3. Elastic Scalability
Azure Cosmos DB offers elastic and automatic scalability, both horizontally (across regions) and vertically (within a region). The MongoDB API inherits this capability, ensuring your application can handle spikes in traffic with ease.

4. Comprehensive SLAs
Azure Cosmos DB guarantees high availability, low latency, and consistent throughput with industry-leading SLAs. This reliability ensures your MongoDB-based workloads perform optimally.

5. Rich Query Support
With the MongoDB API, you can execute advanced queries, including aggregation pipelines, on your Cosmos DB data. This enables powerful data analysis and reporting capabilities.

6. Serverless and Provisioned Throughput
Choose between serverless (pay-as-you-go) or provisioned throughput modes to optimize cost and performance based on your application’s requirements.

7. Advanced Security
Cosmos DB ensures your data is secure with features like private endpoints, VNET integration, encryption-at-rest, and role-based access control (RBAC). The MongoDB API benefits from the same enterprise-grade security features.

Use Cases for Azure Cosmos DB with MongoDB API

1. Global Applications

Applications with users spread across different regions benefit from Cosmos DB’s multi-region distribution. For example, an e-commerce platform serving customers worldwide can deliver low-latency experiences regardless of location.

2. Modernizing MongoDB Workloads

Organizations looking to modernize their MongoDB workloads can migrate to Cosmos DB without rewriting code. This allows them to leverage advanced Azure features like autoscaling, high availability, and global replication.

3. IoT and Real-Time Analytics

With its support for high-throughput and low-latency operations, Cosmos DB with MongoDB API is ideal for IoT applications and real-time analytics use cases.

4. Hybrid Cloud and Backup

Use Azure Cosmos DB with MongoDB API as a hybrid cloud solution or as a backup option for your on-premises MongoDB workloads. Its seamless integration simplifies data replication and disaster recovery.

Getting Started with Azure Cosmos DB and MongoDB API

Here’s how you can begin using Azure Cosmos DB with MongoDB API:

  1. Create a Cosmos DB Account with MongoDB API
  • Navigate to the Azure Portal.
  • Click Create a Resource > Databases > Azure Cosmos DB for MongoDB API.
  • Configure your account settings, including the database name, region, and throughput.
  1. Connect Your Application
  • Use your existing MongoDB drivers and tools to connect to Cosmos DB. Update your connection string to point to the Cosmos DB MongoDB endpoint.
  • Example connection string:
  • const { MongoClient } = require(“mongodb”);
  • const uri = “mongodb://<your-cosmosdb-uri>:<port>/?ssl=true&replicaSet=globaldb&retrywrites=false”;
  • const client = new MongoClient(uri);
  1. Migrate Your Data
  • Use tools like mongodump and mongorestore to migrate data from an existing MongoDB database to Cosmos DB. For example:
  • mongodump –uri mongodb://<source-uri>
  • mongorestore –uri mongodb://<cosmosdb-uri>
  1. Test and Optimize
  • Test your application to ensure everything works as expected. Utilize Cosmos DB’s insights and metrics for performance monitoring and optimization.

Pricing Considerations

Azure Cosmos DB pricing depends on throughput, storage, and features like geo-replication. The MongoDB API is fully integrated into Cosmos DB’s pricing model. You can choose between:

  • Provisioned Throughput: Best for predictable workloads.
  • Serverless: Best for infrequent or unpredictable workloads.

Use the Azure Pricing Calculator to estimate your costs.

Create Your vCore-based Azure Cosmos DB for MongoDB Account

Let’s dive in and see how easy it is to create your vCore-based Azure Cosmos DB for MongoDB account using the Azure portal. Follow these steps to get started:

  1. Sign in to the Azure portal.
  2. In the left-hand menu, select on the Create a resource

  1. In the Create a resource window, search for Azure Cosmos DB.

  1. In the Marketplace window, select Azure Cosmos DB and select the Create

  1. In the Azure Cosmos DB window, select the Create

  1. In the Create Azure Cosmos DB Account window, select Azure Cosmos DB for MongoDB, and select the Create

  1. In the Create Azure Cosmos DB Account Choose Architecture window, select vCore cluster (Recommended) and select the Create

  1. In the Create Azure Cosmos DB for MongoDB cluster window, fill in the details (leave other’s blank):

Basics tab:

Field Action
Subscription Choose the Azure subscription that you want to use for this Cosmos DB account.
Resource group If your Azure account is allowed, create a new resource group, if not, select a nonproduction resource group.
Cluster name Enter a unique name to identify your Cosmos DB cluster. This name must be unique across all Azure clusters.
 

 

Location

Select a geographic location. Depending on your subscriptions, some regions might not allow you to create the cluster. Contact your Azure Administrator if you have any issues.
Cluster tier Select Configure. Familiarize yourself with all the settings on the Scale window. Select Free tier and Save.
MongoDB version 7.0
Admin username cosmosClusterAdmin
Password Enter a strong password.
Confirm password Enter the same password. Copy that password somewhere safe, you need it later.

 

Networking tab:

Field Action
Connectivity method public access (allowed IP addresses)
 

Firewall rules

Select the Allow public access from Azure services and resources within Azure to this cluster checkbox. This will allow you to use the Azure portal Mongo Shell.
 

Firewall rules

Manually add all the IP ranges that you would like to grant access to your Cosmos DB account by selecting a Rule Name, a Start IP address and an End IP address.

In the Review + create tab, review your account settings, and then select the Create button.

It takes 5+ minutes for the vCore-based Azure Cosmos DB for MongoDB account to be created. After resource deployment click on the GO TO Resource

 In the Ǫuick start window, select Mongo Shell.

Enter the password for the admin user you created when you created the vCore-based Azure Cosmos DB for MongoDB account in the Mongo Shell window.

After entering the password

Note: You can copy the connection string and the credential

To test the connection, let’s run some MongoDB commands.

Conclusion

Azure Cosmos DB with MongoDB API combines the power of Cosmos DB’s globally distributed architecture with the ease and familiarity of MongoDB. It’s a perfect solution for developers and organizations seeking to modernize their workloads, build globally available applications, or simplify database management.

Whether you’re migrating an existing MongoDB workload or starting a new project, Azure Cosmos DB with MongoDB API offers the flexibility, scalability, and performance you need. Start exploring today and unlock the potential of your applications in the cloud.

Enhance Your Productivity with Microsoft Copilot

  • Effortless Integration
  • AI-Powered Assistance
Get Started Now

About CloudThat

CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.

CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training PartnerAWS Migration PartnerAWS Data and Analytics PartnerAWS DevOps Competency PartnerAWS GenAI Competency PartnerAmazon QuickSight Service Delivery PartnerAmazon EKS Service Delivery Partner AWS Microsoft Workload PartnersAmazon EC2 Service Delivery PartnerAmazon ECS Service Delivery PartnerAWS Glue Service Delivery PartnerAmazon Redshift Service Delivery PartnerAWS Control Tower Service Delivery PartnerAWS WAF Service Delivery PartnerAmazon CloudFrontAmazon OpenSearchAWS DMS and many more.

FAQs

1. What is Azure Cosmos DB for MongoDB?

ANS: – Azure Cosmos DB for MongoDB is a fully managed NoSQL database service that supports MongoDB API. It allows developers to use MongoDB tools, drivers, and frameworks while benefiting from Azure Cosmos DB’s globally distributed, highly scalable, and low-latency database platform.

2. How do I connect to Azure Cosmos DB using the MongoDB API?

ANS: – To connect:

  1. Retrieve the Connection String:
    • Go to your Azure Cosmos DB account in the Azure portal.
    • Navigate to Connection String under the “Settings” section.
  2. Use a MongoDB Driver:
    Use MongoDB-compatible drivers or tools (e.g., MongoDB shell, Mongoose, Compass):
  3. mongodb://<username>:<password>@<cosmos-db-endpoint>:<port>/<database-name>?ssl=true&replicaSet=globaldb

3. What versions of MongoDB are supported by Azure Cosmos DB?

ANS: – Azure Cosmos DB for MongoDB supports the following versions:

  • MongoDB 3.6
  • MongoDB 4.0
  • MongoDB 4.2
    The version can be selected when creating the Azure Cosmos DB account.

4. What are the benefits of using Azure Cosmos DB with the MongoDB API?

ANS: –

  • Global Distribution: Multi-region replication with automatic failover.
  • Elastic Scalability: Scales throughput and storage independently.
  • Low Latency: Guarantees single-digit millisecond response times.
  • Compatibility: Works with existing MongoDB drivers and tools.
  • SLAs: Comprehensive SLAs for availability, throughput, latency, and consistency.

5. Can I migrate an existing MongoDB database to Azure Cosmos DB?

ANS: – Yes, you can migrate MongoDB data to Azure Cosmos DB using the following tools:

  • Azure Data Migration Service (DMS): For seamless, offline, or online migrations.
  • mongodump/mongorestore Tools: Use the dump and restore approach to transfer data manually.
  • Third-party tools: Tools like Studio 3T or custom scripts can also be used.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!