为 Azure Cosmos 容器定义唯一键Define unique keys for an Azure Cosmos container

本文介绍在创建 Azure Cosmos 容器时定义唯一键的不同方法。This article presents the different ways to define unique keys when creating an Azure Cosmos container. 目前,可以通过使用 Azure 门户或通过其中一个 SDK 来执行此操作。It's currently possible to perform this operation either by using the Azure portal or through one of the SDKs.

使用 Azure 门户Use the Azure portal

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 创建新的 Azure Cosmos 帐户或选择现有帐户。Create a new Azure Cosmos account or select an existing one.

  3. 打开“数据资源管理器”窗格,选择要使用的容器。Open the Data Explorer pane and select the container that you want to work on.

  4. 单击“新建容器”。Click on New Container.

  5. 在“添加容器”对话框中,单击“+ 添加唯一键”,添加唯一键条目 。In the Add Container dialog, click on + Add unique key to add a unique key entry.

  6. 输入唯一键约束的路径Enter the path(s) of the unique key constraint

  7. 如果需要,可通过单击“+ 添加唯一键”,添加更多唯一键条目If needed, add more unique key entries by clicking on + Add unique key

    Azure 门户中唯一键约束条目的屏幕截图

使用 PowerShellUse Powershell

要创建具有唯一键的容器,请参阅创建具有唯一键和 TTL 的 Azure Cosmos 容器To create a container with unique keys see, Create an Azure Cosmos container with unique key and TTL

使用 .NET SDKUse the .NET SDK

使用 .NET SDK v2 创建新的容器时,可使用 UniqueKeyPolicy 对象定义唯一键约束。When creating a new container using the .NET SDK v2, a UniqueKeyPolicy object can be used to define unique key constraints.

client.CreateDocumentCollectionAsync(UriFactory.CreateDatabaseUri("database"), new DocumentCollection
{
    Id = "container",
    PartitionKey = new PartitionKeyDefinition { Paths = new Collection<string>(new List<string> { "/myPartitionKey" }) },
    UniqueKeyPolicy = new UniqueKeyPolicy
    {
        UniqueKeys = new Collection<UniqueKey>(new List<UniqueKey>
        {
            new UniqueKey { Paths = new Collection<string>(new List<string> { "/firstName", "/lastName", "/emailAddress" }) },
            new UniqueKey { Paths = new Collection<string>(new List<string> { "/address/zipCode" }) }
        })
    }
});

使用 Java SDKUse the Java SDK

使用 Java SDK 创建新的容器时,可使用 UniqueKeyPolicy 对象定义唯一键约束。When creating a new container using the Java SDK, a UniqueKeyPolicy object can be used to define unique key constraints.

// create a new DocumentCollection object
DocumentCollection container = new DocumentCollection();
container.setId("container");

// create array of strings and populate them with the unique key paths
Collection<String> uniqueKey1Paths = new ArrayList<String>();
uniqueKey1Paths.add("/firstName");
uniqueKey1Paths.add("/lastName");
uniqueKey1Paths.add("/emailAddress");
Collection<String> uniqueKey2Paths = new ArrayList<String>();
uniqueKey2Paths.add("/address/zipCode");

// create UniqueKey objects and set their paths
UniqueKey uniqueKey1 = new UniqueKey();
UniqueKey uniqueKey2 = new UniqueKey();
uniqueKey1.setPaths(uniqueKey1Paths);
uniqueKey2.setPaths(uniqueKey2Paths);

// create a new UniqueKeyPolicy object and set its unique keys
UniqueKeyPolicy uniqueKeyPolicy = new UniqueKeyPolicy();
Collection<UniqueKey> uniqueKeys = new ArrayList<UniqueKey>();
uniqueKeys.add(uniqueKey1);
uniqueKeys.add(uniqueKey2);
uniqueKeyPolicy.setUniqueKeys(uniqueKeys);

// set the unique key policy
container.setUniqueKeyPolicy(uniqueKeyPolicy);

// create the container
client.createCollection(String.format("/dbs/%s", "database"), container, null);

使用 Node.js SDKUse the Node.js SDK

使用 Node.js SDK 创建新的容器时,可使用 UniqueKeyPolicy 对象定义唯一键约束。When creating a new container using the Node.js SDK, a UniqueKeyPolicy object can be used to define unique key constraints.

client.database('database').containers.create({
    id: 'container',
    uniqueKeyPolicy: {
        uniqueKeys: [
            { paths: ['/firstName', '/lastName', '/emailAddress'] },
            { paths: ['/address/zipCode'] }
        ]
    }
});

使用 Python SDKUse the Python SDK

使用 Python SDK 创建新的容器时,可将唯一键约束指定为作为参数传递的字典的一部分。When creating a new container using the Python SDK, unique key constraints can be specified as part of the dictionary passed as parameter.

client.CreateContainer('dbs/' + config['DATABASE'], {
    'id': 'container',
    'uniqueKeyPolicy': {
        'uniqueKeys': [
            {'paths': ['/firstName', '/lastName', '/emailAddress']},
            {'paths': ['/address/zipCode']}
        ]
    }
})

后续步骤Next steps