UniqueKeyPolicy Class

Definition

Represents the unique key policy configuration for specifying uniqueness constraints on documents in the collection in the Azure Cosmos DB service.

public sealed class UniqueKeyPolicy
type UniqueKeyPolicy = class
Public NotInheritable Class UniqueKeyPolicy
Inheritance
System.Object
UniqueKeyPolicy

Examples

var collectionSpec = new DocumentCollection { Id = "Collection with unique keys", UniqueKeyPolicy = new UniqueKeyPolicy { UniqueKeys = new Collection<UniqueKey> { // pair </name/first, name/last> is unique. new UniqueKey { Paths = new Collection<string> { "/name/first", "/name/last" } }, // /address is unique. new UniqueKey { Paths = new Collection<string> { "/address" } }, } } }; DocumentCollection collection = await client.CreateDocumentCollectionAsync(databaseLink, collectionSpec });

var doc = JObject.Parse("{"name": { "first": "John", "last": "Smith" }, "alias":"johnsmith" }"); await client.CreateDocumentAsync(collection.SelfLink, doc);

doc = JObject.Parse("{"name": { "first": "James", "last": "Smith" }, "alias":"jamessmith" }"); await client.CreateDocumentAsync(collection.SelfLink, doc);

try { // Error: first+last name is not unique. doc = JObject.Parse("{"name": { "first": "John", "last": "Smith" }, "alias":"johnsmith1" }"); await client.CreateDocumentAsync(collection.SelfLink, doc); throw new Exception("CreateDocumentAsync should have thrown exception/conflict"); } catch (DocumentClientException ex) { if (ex.StatusCode != System.Net.HttpStatusCode.Conflict) throw; }

try { // Error: alias is not unique. doc = JObject.Parse("{"name": { "first": "James Jr", "last": "Smith" }, "alias":"jamessmith" }"); await client.CreateDocumentAsync(collection.SelfLink, doc); throw new Exception("CreateDocumentAsync should have thrown exception/conflict"); } catch (DocumentClientException ex) { if (ex.StatusCode != System.Net.HttpStatusCode.Conflict) throw; }

Remarks

Unique key policies add a layer of data integrity to an Azure Cosmos container. They cannot be modified once the container is created.

Refer to for additional information on how to specify unique key policies.

Constructors

UniqueKeyPolicy()

Properties

UniqueKeys

Gets collection of UniqueKey that guarantee uniqueness of documents in collection in the Azure Cosmos DB service.

Applies to

See also