|
| 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 | + |
| 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) |
0 commit comments