UniqueKeyPolicy Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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 : Microsoft.Azure.Documents.JsonSerializable
type UniqueKeyPolicy = class
inherit JsonSerializable
Public NotInheritable Class UniqueKeyPolicy
Inherits JsonSerializable
- Inheritance
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. |
Properties
UniqueKeys |
Gets or sets collection of UniqueKey that guarantee uniqueness of documents in collection in the Azure Cosmos DB service. |
Methods
Equals(Object) | |
GetHashCode() | |
LoadFrom(JsonReader, JsonSerializerSettings) |
Loads the object from the specified JSON reader in the Azure Cosmos DB service. (Inherited from JsonSerializable) |
LoadFrom(JsonReader) |
Loads the object from the specified JSON reader in the Azure Cosmos DB service. (Inherited from JsonSerializable) |
SaveTo(Stream, SerializationFormattingPolicy, JsonSerializerSettings) |
Saves the object to the specified stream in the Azure Cosmos DB service. (Inherited from JsonSerializable) |
SaveTo(Stream, SerializationFormattingPolicy) |
Saves the object to the specified stream in the Azure Cosmos DB service. (Inherited from JsonSerializable) |
ToString() |
Returns the string representation of the object in the Azure Cosmos DB service. (Inherited from JsonSerializable) |