Unique Keys

From Ilianko

← Back to DP-420 Terminology Reference

Category: Data Modeling & Partitioning

Summary: Container-level constraint enforcing uniqueness of property paths per logical partition.

Overview

Unique keys are one or more paths defined at container creation time that Azure Cosmos DB enforces as unique within each logical partition, ensuring no two items in the same logical partition share the same combination of unique key values. Unique key constraints are useful for enforcing business rules like preventing duplicate usernames or emails within a tenant's partition.

//.NET SDK
await client.GetDatabase("your-database").DefineContainer(name: "your-container", partitionKeyPath: "/your-partition-key")
    .WithUniqueKey()
       .Path("/email")
       .Path("/region")
    .Attach()
    .CreateIfNotExistsAsync();
//JSON for unique key policy
"uniqueKeyPolicy": {
    "uniqueKeys": [
        {
            "paths": [
                "/email",
                "/region"
            ]
        }
    ]
}

More Info & Links

DP-420 Style Practice Questions

Q1. What do unique keys enforce in an Azure Cosmos DB container?

  • A. Uniqueness of specified property combinations within each logical partition
  • B. Uniqueness of the partition key across the whole container
  • C. A single fixed schema for all items
  • D. Encryption of specific properties

Q2. When must unique keys be defined for a container?

  • A. At container creation time
  • B. At any time after creation via an update
  • C. Only when using the MongoDB API
  • D. Only when TTL is enabled

Answer Key

Answers are listed at the very end so you can attempt the questions first.

  • Q1 (Unique Keys): A. Uniqueness of specified property combinations within each logical partition — Unique keys guarantee that a chosen set of properties is unique within each logical partition.
  • Q2 (Unique Keys): A. At container creation time — Unique key policies must be specified when the container is created and cannot be changed afterward.

DP-420: Designing and Implementing Cloud-Native Applications Using Microsoft Azure Cosmos DB — term detail page