Azure-docs: Create unique key via REST API

Created on 25 Sep 2019  Â·  5Comments  Â·  Source: MicrosoftDocs/azure-docs

I'm trying to create our CosmosDB configuration via scripts that call the REST API. To create collections I'm following the documentation here, however it doesn't mention anything about how to specify unique keys.

Is there currently an undocumented way to do this?


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Pri2 cosmos-dsvc cxp product-question triaged

Most helpful comment

I reached out to AskCosmosDB as suggested and they provided me with a helpful answer:

using DocumentDB Studio you can click a collection to see the JSON that it's made up from. within this JSON is the uniqueKeyPolicy property that i needed to append to my JSON body within my request body on the REST API.

my old JSON body was:

{
    "id": "SomeCollection",
    "partitionKey": {
        "paths": ["/somePartitionKey"],
        "kind": "Hash"
    }
}

and my new, working JSON body is:

{
    "id": "SomeCollection",
    "partitionKey": {
        "paths": ["/somePartitionKey"],
        "kind": "Hash"
    },
    "uniqueKeyPolicy": {
        "uniqueKeys": [{
            "paths": ["/someCompositeUniqueKey1",
            "/someCompositeUniqueKey2"]
        }]
    }
}

thanks for the help!

All 5 comments

@ctoph13 Thank you for bringing this to our attention. The best course of action is to reach out to the Product Group directly at AskCosmosDB. In my investigation, Unique Keys are not exposed via the REST API as they augment the defined Partition Key for a given collection and are part of the service layer, not a specific collection property. @ThomasWeiss @SnehaGunda for awareness and additional input.

I reached out to AskCosmosDB as suggested and they provided me with a helpful answer:

using DocumentDB Studio you can click a collection to see the JSON that it's made up from. within this JSON is the uniqueKeyPolicy property that i needed to append to my JSON body within my request body on the REST API.

my old JSON body was:

{
    "id": "SomeCollection",
    "partitionKey": {
        "paths": ["/somePartitionKey"],
        "kind": "Hash"
    }
}

and my new, working JSON body is:

{
    "id": "SomeCollection",
    "partitionKey": {
        "paths": ["/somePartitionKey"],
        "kind": "Hash"
    },
    "uniqueKeyPolicy": {
        "uniqueKeys": [{
            "paths": ["/someCompositeUniqueKey1",
            "/someCompositeUniqueKey2"]
        }]
    }
}

thanks for the help!

@ctoph13 Thank you so much for following up and posting the solution, as it will be very helpful to those looking for the same or similar solution.

Could you please add this to the official document? This is especially relevant if you are using ARM templates to set the unique key policies and I havent found any kind of official documentation regarding that.

@florianeidner We're actually in the process of updating our ARM templates samples to include unique key policies, so this should land in the near future. Thanks!

Was this page helpful?
0 / 5 - 0 ratings