Skip to content

Commit 2183206

Browse files
authored
Merge pull request #75727 from ThomasWeiss/cosmosdb-how-to-unique-keys2
Added section on how to define unique keys
2 parents ee1e969 + e5a2929 commit 2183206

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

articles/cosmos-db/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@
512512
href: data-explorer.md
513513
- name: Manage indexing policies
514514
href: how-to-manage-indexing-policy.md
515+
- name: Define unique keys
516+
href: how-to-define-unique-keys.md
515517
- name: SQL subquery examples
516518
displayName: subquery, exists
517519
href: how-to-sql-subquery.md
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
title: Define unique keys for an Azure Cosmos container
3+
description: Learn how to define unique keys for an Azure Cosmos container
4+
author: ThomasWeiss
5+
ms.service: cosmos-db
6+
ms.topic: sample
7+
ms.date: 5/3/2019
8+
ms.author: thweiss
9+
---
10+
11+
# Define unique keys for an Azure Cosmos container
12+
13+
This article presents the different ways to define [unique keys](unique-keys.md) when creating an Azure Cosmos container. It's currently possible to perform this operation either by using the Azure portal or through one of the SDKs.
14+
15+
## Use the Azure portal
16+
17+
1. Sign in to the [Azure portal](https://portal.azure.com/).
18+
19+
1. [Create a new Azure Cosmos account](create-sql-api-dotnet.md#create-account) or select an existing one.
20+
21+
1. Open the **Data Explorer** pane and select the container that you want to work on.
22+
23+
1. Click on **New Container**.
24+
25+
1. In the **Add Container** dialog, click on **+ Add unique key** to add a unique key entry.
26+
27+
1. Enter the path(s) of the unique key constraint
28+
29+
1. If needed, add more unique key entries by clicking on **+ Add unique key**
30+
31+
![Screenshot of unique key constraint entry on Azure portal](./media/how-to-define-unique-keys/unique-keys-portal.png)
32+
33+
## Use the .NET SDK V2
34+
35+
When creating a new container using the [.NET SDK v2](https://www.nuget.org/packages/Microsoft.Azure.DocumentDB/), a `UniqueKeyPolicy` object can be used to define unique key constraints.
36+
37+
```csharp
38+
client.CreateDocumentCollectionAsync(UriFactory.CreateDatabaseUri("database"), new DocumentCollection
39+
{
40+
Id = "container",
41+
UniqueKeyPolicy = new UniqueKeyPolicy
42+
{
43+
UniqueKeys = new Collection<UniqueKey>(new List<UniqueKey>
44+
{
45+
new UniqueKey { Paths = new Collection<string>(new List<string> { "/firstName", "/lastName", "emailAddress" }) },
46+
new UniqueKey { Paths = new Collection<string>(new List<string> { "/address/zipCode" }) }
47+
})
48+
}
49+
});
50+
```
51+
52+
## Use the Java SDK
53+
54+
When creating a new container using the [Java SDK](https://mvnrepository.com/artifact/com.microsoft.azure/azure-cosmosdb), a `UniqueKeyPolicy` object can be used to define unique key constraints.
55+
56+
```java
57+
// create a new DocumentCollection object
58+
DocumentCollection container = new DocumentCollection();
59+
container.setId("container");
60+
// create array of strings and populate them with the unique key paths
61+
Collection<String> uniqueKey1Paths = new ArrayList<String>();
62+
uniqueKey1Paths.add("/firstName");
63+
uniqueKey1Paths.add("/lastName");
64+
uniqueKey1Paths.add("/emailAddress");
65+
Collection<String> uniqueKey2Paths = new ArrayList<String>();
66+
uniqueKey2Paths.add("/address/zipCode");
67+
// create UniqueKey objects and set their paths
68+
UniqueKey uniqueKey1 = new UniqueKey();
69+
UniqueKey uniqueKey2 = new UniqueKey();
70+
uniqueKey1.setPaths(uniqueKey1Paths);
71+
uniqueKey2.setPaths(uniqueKey2Paths);
72+
// create a new UniqueKeyPolicy object and set its unique keys
73+
UniqueKeyPolicy uniqueKeyPolicy = new UniqueKeyPolicy();
74+
Collection<UniqueKey> uniqueKeys = new ArrayList<UniqueKey>();
75+
uniqueKeys.add(uniqueKey1);
76+
uniqueKeys.add(uniqueKey2);
77+
uniqueKeyPolicy.setUniqueKeys(uniqueKeys);
78+
// set the unique key policy
79+
container.setUniqueKeyPolicy(uniqueKeyPolicy);
80+
// create the container
81+
client.createCollection(String.format("/dbs/%s", "database"), container, null);
82+
```
83+
84+
## Use the Node.js SDK
85+
86+
When creating a new container using the [Node.js SDK](https://www.npmjs.com/package/@azure/cosmos), a `UniqueKeyPolicy` object can be used to define unique key constraints.
87+
88+
```javascript
89+
client.database('database').containers.create({
90+
id: 'container',
91+
uniqueKeyPolicy: {
92+
uniqueKeys: [
93+
{ paths: ['/firstName', '/lastName', '/emailAddress'] },
94+
{ paths: ['/address/zipCode'] }
95+
]
96+
}
97+
});
98+
```
99+
100+
## Use the Python SDK
101+
102+
When creating a new container using the [Python SDK](https://pypi.org/project/azure-cosmos/), unique key constraints can be specified as part of the dictionary passed as parameter.
103+
104+
```python
105+
client.CreateContainer('dbs/' + config['DATABASE'], {
106+
'id': 'container',
107+
'uniqueKeyPolicy': {
108+
'uniqueKeys': [
109+
{ 'paths': ['/firstName', '/lastName', '/emailAddress'] },
110+
{ 'paths': ['/address/zipCode'] }
111+
]
112+
}
113+
})
114+
```
115+
116+
## Next steps
117+
118+
- Learn more about [partitioning](partition-data.md)
119+
- Explore [how indexing works](index-overview.md)
9.75 KB
Loading

articles/cosmos-db/unique-keys.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ You can define unique keys only when you create an Azure Cosmos container. A uni
5050

5151
## Next steps
5252

53-
* Learn more about [logical partitions](partition-data.md).
53+
* Learn more about [logical partitions](partition-data.md)
54+
* Explore [how to define unique keys](how-to-define-unique-keys.md) when creating a container

0 commit comments

Comments
 (0)