How to show unique keys on Cosmos DB container?
Asked Answered
R

5

4

This link implies that unique keys can be seen in a Cosmos DB container by looking at the settings. However I can't seem to find them using both the portal and the storage explorer. How can you view the unique keys on an existing Cosmos DB container? I have a document that fails to load due to a key violation which should be impossible so I need to confirm what the keys are.

Reconstruct answered 26/11, 2018 at 23:7 Comment(0)
A
10

A slightly easier way to view your Cosmos DB unique keys is to view the ARM template for your resource.

On your Cosmos DB account, click Settings/ Export Template- let the template be generated and view online once complete. You will find them under the "uniqueKeyPolicy" label.

Almatadema answered 2/1, 2020 at 14:32 Comment(1)
I found it by scrolling down on the left menu to automation and click the item "Export Template"Schear
H
3

Azure Portal - Cosmos DB - Export Template Location

Here you can view the ARM template in your Azure Portal, and as the winner comment says You will find the unique keys under the "uniqueKeyPolicy" label.

uniqueKeyPolicy in the ARM Template

Hedron answered 7/1, 2023 at 14:29 Comment(0)
J
1

Based on this blob, unique keys policy should be visible like below:

"uniqueKeyPolicy": {
    "uniqueKeys": [
      {
        "paths": [
          "/name",
          "/country"
        ]
      },
      {
        "paths": [
          "/users/title"
        ]
      }
    ]
  }

However, I could not see it on the portal as same as you. Maybe it's a bug here.

You could use cosmos db sdk as a workaround to get the unique keys policy, please see my java sample code.

ResourceResponse<DocumentCollection> response1 = documentClient.readCollection("dbs/db/colls/test", null);
DocumentCollection coll =response1.getResource();
UniqueKeyPolicy uniqueKeyPolicy = coll.getUniqueKeyPolicy();
Collection<UniqueKey> uniqueKeyCollections = uniqueKeyPolicy.getUniqueKeys();

for(UniqueKey uniqueKey : uniqueKeyCollections){
    System.out.println(uniqueKey.getPaths());
}
Jonna answered 27/11, 2018 at 1:31 Comment(1)
I saw that article as well but the author fails to say how you actually view the Json definition of the container. The fact that this seems to be so basic but not easily solved makes me question the reliability of the Cosmos DB solution. I will give your idea a try when I can but it seems really wasteful to spin up a project just to get the indexes.Reconstruct
R
0

Here is the basic code that worked for me. The code that writes the collection is output in Json format. I think this is similar to what you see in the portal but it skips or omits the uniqueKeyPolicy information.

As a side note I think I found a bug or odd behavior. Inserting a new document can throw unique index constraint violation but updates do not.

            this.EndpointUrl = ConfigurationManager.AppSettings["EndpointUrl"];
        this.PrimaryKey = ConfigurationManager.AppSettings["PrimaryKey"];
        string dbname = ConfigurationManager.AppSettings["dbname"];
        string containername = ConfigurationManager.AppSettings["containername"];
        this.client = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);
        DocumentCollection collection = await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(dbname, containername));
        Console.WriteLine("\n4. Found Collection \n{0}\n", collection);
Reconstruct answered 27/11, 2018 at 16:45 Comment(0)
P
0

Support for showing unique key policy in collection properties will be added soon. Meanwhile you can use DocumentDBStudio to see unique keys in collection. Once unique key policy is set, it cannot be modified.

WRT odd behavior, can you please share full isolated repro and explain expected and actual behavior.

Paraclete answered 27/11, 2018 at 17:53 Comment(3)
I don't have a clean example to share but what I consider odd or a bug is that the unique index constraint violation is NOT thrown during an update action like UpsertDocumentAsync. To reproduce the problem you need to start with a container that has a unique key policy (ex. email_address). Then use CreateDocumentAsync to add a new document and note the Id. Now use UpsertDocumentAsync on that Id but use a document that lacks the unique key. The UpsertDocumentAsync will work. If you try to CreateDocumentAsync with a document that lacks the key it will fail.Reconstruct
Soon, and now is 2022...Defrock
Soon, and now is 2024-10Chickabiddy

© 2022 - 2024 — McMap. All rights reserved.