Azure CosmosDB (DocumentDB) Mongo Collection - Examples and best practices | Shisho Dojo (2024)

This page shows how to write Terraform and Azure Resource Manager for CosmosDB (DocumentDB) Mongo Collection and write them securely.

azurerm_cosmosdb_mongo_collection (Terraform)

The Mongo Collection in CosmosDB (DocumentDB) can be configured in Terraform with the resource name azurerm_cosmosdb_mongo_collection. The following sections describe 10 examples of how to use the resource and its parameters.

Example Usage from GitHub

azure.tf#L72

resource "azurerm_cosmosdb_mongo_collection" "ngc" { name = "Sessions" resource_group_name = azurerm_resource_group.ngc.name account_name = azurerm_cosmosdb_account.ngc.name database_name = azurerm_cosmosdb_mongo_database.ngc_sessions.name

Find out how to use this setting securely with Shisho Cloud

cosmosdb-mongodb-collection.tf#L4

resource "azurerm_cosmosdb_mongo_collection" "mongo_collection_static" { name = var.collection_name resource_group_name = var.resource_group_name account_name = var.account_name database_name = var.db_name default_ttl_seconds = var.default_ttl_seconds

Find out how to use this setting securely with Shisho Cloud

cosmosdb_mongo_collection_test.tf#L122

resource "azurerm_cosmosdb_mongo_collection" "non-usage_autoscale" { name = "tfex-cosmos-mongo-db" resource_group_name = azurerm_cosmosdb_account.example.resource_group_name account_name = azurerm_cosmosdb_account.example.name database_name = azurerm_cosmosdb_mongo_database.non-usage_autoscale.name

Find out how to use this setting securely with Shisho Cloud

cosmosdb_mongo_collection_test.tf#L122

resource "azurerm_cosmosdb_mongo_collection" "non-usage_autoscale" { name = "tfex-cosmos-mongo-db" resource_group_name = azurerm_cosmosdb_account.example.resource_group_name account_name = azurerm_cosmosdb_account.example.name database_name = azurerm_cosmosdb_mongo_database.non-usage_autoscale.name

Find out how to use this setting securely with Shisho Cloud

main.tf#L60

resource "azurerm_cosmosdb_mongo_collection" "questions" { name = "questions" resource_group_name = azurerm_resource_group.rg.name account_name = azurerm_cosmosdb_account.default.name database_name = azurerm_cosmosdb_mongo_database.default.name

Find out how to use this setting securely with Shisho Cloud

mongo_collection.tf#L2

resource "azurerm_cosmosdb_mongo_collection" "collection" { name = var.settings.name resource_group_name = var.resource_group_name account_name = var.cosmosdb_account_name database_name = var.database_name shard_key = var.settings.shard_key

Find out how to use this setting securely with Shisho Cloud

cosmos-db-mongo-collection.tf#L1

resource "azurerm_cosmosdb_mongo_collection" "event_collection" { name = "events" resource_group_name = azurerm_resource_group.rg.name account_name = azurerm_cosmosdb_account.db_account.name database_name = azurerm_cosmosdb_mongo_database.mongo_db.name

Find out how to use this setting securely with Shisho Cloud

mongo_collection.tf#L2

resource "azurerm_cosmosdb_mongo_collection" "collection" { name = var.settings.name resource_group_name = var.resource_group_name account_name = var.cosmosdb_account_name database_name = var.database_name shard_key = var.settings.shard_key

Find out how to use this setting securely with Shisho Cloud

mongo_collection.tf#L2

resource "azurerm_cosmosdb_mongo_collection" "collection" { name = var.settings.name resource_group_name = var.resource_group_name account_name = var.cosmosdb_account_name database_name = var.database_name shard_key = var.settings.shard_key

Find out how to use this setting securely with Shisho Cloud

main.tf#L24

resource "azurerm_cosmosdb_mongo_collection" "mongodb" { count = length(var.collections) name = var.collections[count.index].name default_ttl_seconds = var.collections[count.index].default_ttl_seconds

Find out how to use this setting securely with Shisho Cloud

Review your Terraform file for Azure best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Explanation in Terraform Registry

Manages a Mongo Collection within a Cosmos DB Account.

Microsoft.DocumentDB/databaseAccounts/mongodbDatabases/collections (Azure Resource Manager)

The databaseAccounts/mongodbDatabases/collections in Microsoft.DocumentDB can be configured in Azure Resource Manager with the resource name Microsoft.DocumentDB/databaseAccounts/mongodbDatabases/collections. The following sections describe how to use the resource and its parameters.

Example Usage from GitHub

An example could not be found in GitHub.

Parameters

  • apiVersion required - string
  • location optional - string

    The location of the resource group to which the resource belongs.

  • name required - string

    Cosmos DB collection name.

  • properties required
      • options optional
          • autoscaleSettings optional
              • maxThroughput optional - integer

                Represents maximum throughput, the resource can scale up to.

          • throughput optional - integer

            Request Units per second. For example, "throughput": 10000.

      • resource required
          • analyticalStorageTtl optional - integer

            Analytical TTL.

          • id required - string

            Name of the Cosmos DB MongoDB collection

          • indexes optional array
              • key optional
                  • keys optional - array

                    List of keys for each MongoDB collection in the Azure Cosmos DB service

              • options optional
                  • expireAfterSeconds optional - integer

                    Expire after seconds

                  • unique optional - boolean

                    Is unique or not

          • shardKey optional - string

            The shard key and partition kind pair, only support "Hash" partition kind

  • tags optional - string

    Tags are a list of key-value pairs that describe the resource. These tags can be used in viewing and grouping this resource (across resource groups). A maximum of 15 tags can be provided for a resource. Each tag must have a key no greater than 128 characters and value no greater than 256 characters. For example, the default experience for a template type is set with "defaultExperience": "Cassandra". Current "defaultExperience" values also include "Table", "Graph", "DocumentDB", and "MongoDB".

  • type required - string

Frequently asked questions

What is Azure CosmosDB (DocumentDB) Mongo Collection?

Azure CosmosDB (DocumentDB) Mongo Collection is a resource for CosmosDB (DocumentDB) of Microsoft Azure. Settings can be wrote in Terraform.

Where can I find the example code for the Azure CosmosDB (DocumentDB) Mongo Collection?

For Terraform, the cloud-native-conference/Infrastructure, marvin-heiden/sendungsverfolgung and gilyas/infracost source code examples are useful. See the Terraform Example section for further details.

Azure CosmosDB (DocumentDB) Mongo Collection - Examples and best practices | Shisho Dojo (2024)

FAQs

Is Cosmos DB better than MongoDB? ›

MongoDB excels in cost-effectiveness, flexibility, and community support, while Cosmos DB offers global distribution, multi-model support, and automatic scalability. Ultimately, selecting the right database solution is essential to ensure optimal performance, efficiency, and success for your business.

What is the difference between DocumentDB and Cosmos DB? ›

Azure Cosmos DB supports multimodal capabilities, allowing developers to work with multiple data models such as key-value, columnar, document, and graph data in a single database. On the other hand, Amazon DocumentDB only supports document data model, providing a MongoDB-compatible interface.

What are the advantages of MongoDB Atlas over Azure Cosmos DB? ›

Cosmos DB also does not support time-series data. MongoDB, on the other hand, lets you run key-value, graph, and SQL queries against the same data. And, with MongoDB 5.0, you can build and run applications with support for specific time-series data storage and query patterns.

What are the disadvantages of Cosmos DB? ›

One of the main weaknesses of Cosmos DB is that users need to be experienced to tune and configure it well. As a result you can end up with a cost over-run of Cosmos DB is not configured properly. Learning Cosmos DB takes time, it is nothing like regular SQL or MS SQL.

Is Azure Cosmos DB deprecated? ›

Yes, that is correct. The Microsoft Azure CosmosDB Table API has been deprecated and replaced with Azure.

Why is Azure Cosmos DB so expensive? ›

Azure Cosmos DB can be costly due to its robust features, global distribution, and low latency performance. Factors like provisioned throughput, storage, and data transfer contribute to expenses. Users pay for the scalability, flexibility, and advanced capabilities provided by the service.

Is MongoDB same as DocumentDB? ›

DocumentDB is not based on the MongoDB server. Rather it emulates the MongoDB API, and runs on top of Amazon's Aurora backend platform. This creates significant architectural constraints, functionality limitations and broken compatibility.

What is the maximum database size for Azure Cosmos DB? ›

- Maximum storage per Cosmos DB account: 20 TB (soft limit, can be increased upon request). - Maximum request unit (RU) per second for a single partition key value: 10,000 RU/s for dedicated throughput containers and 4,000 RU/s for shared throughput containers.

Should I use Cosmos DB or SQL Server? ›

While MS SQL Server shows superior performance for the same monetary investment, it's crucial to consider the specific requirements of your application. CosmosDB, with its multi-region and multi-master features, is an ideal choice for applications requiring global distribution and high availability.

What is Azure Cosmos DB an example of? ›

Azure Cosmos DB is a fully managed platform as a service (PaaS).

Which Azure Cosmos DB API should you use? ›

If you're currently using Azure Table Storage, the Core (SQL) API would be a better choice. It offers a richer query experience with improved indexing over the Table API. If you don't want to rewrite your application, consider migrating to the Azure Cosmos DB Table API.

What is Azure Cosmos DB good for? ›

Azure Cosmos DB offers a solution for modern apps and intelligent workloads by being very responsive with dynamic and elastic autoscale. It is available in every Azure region and can automatically replicate data closer to users. It has SLA guaranteed low-latency and high availability.

Is MongoDB cheaper than Cosmos DB? ›

MongoDB offers more flexibility in terms of setup. It runs on-prem, any cloud provider (Google Cloud Platform, AWS, Azure and more). We found Azure Cosmos DB to be 93% cheaper for reads and around 20% cheaper for writes than MongoDB Atlas depending on workload characteristics (see TCO table below).

Why is Cosmos DB so slow? ›

Request throttling is the most common reason for slow requests. Azure Cosmos DB throttles requests if they exceed the allocated request units for the database or container. The SDK has built-in logic to retry these requests.

What is the vulnerability of Cosmos DB Azure? ›

The 'CosMiss' vulnerability allowed an unauthenticated user to obtain read and write access to Azure Cosmos DB Notebooks, inject code, and overwrite code – constituting remote code execution (RCE).

Which database is better than MongoDB? ›

As a relational database management system, MySQL has a more rigid structure than MongoDB. MySQL uses a fixed schema and organizes data into a row and table.

What is Cosmos DB best for? ›

Azure Cosmos DB is often used for event sourcing to power event driven architectures using its change feed functionality. The change feed provides downstream microservices the ability to reliably and incrementally read inserts and updates (for example, order events) made to an Azure Cosmos DB.

Is Cosmos DB reliable? ›

Azure Cosmos DB automatically mitigates replica outages by guaranteeing at least three replicas of your data in each Azure region for your account within a four-replica quorum. This guarantee results in an RTO of 0 and an RPO of 0 for individual node outages, without requiring application changes or configurations.

What is the Azure equivalent of MongoDB? ›

Azure Cosmos DB is Microsoft's globally distributed multi-model database service. You can communicate with the Azure Cosmos DB for MongoDB using any of the open-source MongoDB client drivers.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Barbera Armstrong

Last Updated:

Views: 6460

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.