次の方法で共有

为 Azure Cosmos DB 容器定义唯一键

本文介绍在创建 Azure Cosmos DB 容器时定义唯一键的不同方法。 目前,可以通过使用 Azure 门户或通过其中一个 SDK 来执行此操作。

使用 Azure 门户

  1. 登录到 Azure 门户

  2. 创建新的 Azure Cosmos DB 帐户或选择现有的帐户。

  3. 打开“数据资源管理器”窗格,选择要使用的容器。

  4. 单击“新建容器”。

  5. 在“添加容器”对话框中,单击“+ 添加唯一键”,添加唯一键条目 。

  6. 输入唯一键约束的路径

  7. 如果需要,可通过单击“+ 添加唯一键”,添加更多唯一键条目

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

使用 PowerShell

要创建具有唯一键的容器,请参阅创建具有唯一键和 TTL 的 Azure Cosmos DB 容器

使用 .NET SDK

使用 .NET SDK v2 创建新的容器时,可使用 UniqueKeyPolicy 对象定义唯一键约束。

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 SDK

使用 Java SDK 创建新的容器时,可使用 UniqueKeyPolicy 对象定义唯一键约束。

// 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 SDK

使用 Node.js SDK 创建新的容器时,可使用 UniqueKeyPolicy 对象定义唯一键约束。

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

使用 Python SDK

使用 Python SDK 创建新的容器时,可将唯一键约束指定为作为参数传递的字典的一部分。

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

使用 Go SDK

使用 Go SDK 创建新容器时,可以使用 UniqueKeyPolicy 来定义唯一键约束。

db, _ := c.NewDatabase("demodb")

pkDef := azcosmos.PartitionKeyDefinition{
	Paths: []string{"/state"},
	Kind:  azcosmos.PartitionKeyKindHash,
}

uniqueKeyPolicy := azcosmos.UniqueKeyPolicy{
	UniqueKeys: []azcosmos.UniqueKey{
		{
			Paths: []string{"/firstName", "/lastName", "/emailAddress"},
		},
	},
}

_, err = db.CreateContainer(context.Background(), azcosmos.ContainerProperties{
	ID:                     "demo_container",
	PartitionKeyDefinition: pkDef,
	UniqueKeyPolicy:        &uniqueKeyPolicy,
}, nil)

后续步骤