Class UniqueKeyPolicy
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 : JsonSerializable
- Inheritance
-
UniqueKeyPolicy
- Inherited Members
- Extension Methods
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; }
Constructors
UniqueKeyPolicy()
Initializes a new instance of the UniqueKeyPolicy class for the Azure Cosmos DB service.
public UniqueKeyPolicy()
Properties
UniqueKeys
Gets or sets collection of UniqueKey that guarantee uniqueness of documents in collection in the Azure Cosmos DB service.
public Collection<UniqueKey> UniqueKeys { get; set; }