I am new to Cosmos DB and I noticed that we can set the partition key based on needs to scale effectively through code like this:
DocumentCollection myCollection = new DocumentCollection();
myCollection.Id = "coll";
myCollection.PartitionKey.Paths.Add("/deviceId");
Question is can we change the partition key later on after we created the collection and specified the partition key? As I may find out that the choice of partition key is not proper later.
We can't change the partition key for this container anymore. You can only set it when you create a new container. If you do need to change a partition key, you need to create a new container and migrate your data to that one. A partition key consists of a path.
In this lab, you will create multiple Azure Cosmos DB containers. Some of the containers will be unlimited and configured with a partition key, while others will be fixed-sized. You will then use the SQL API and Java Async SDK to query specific containers using a single partition key or across multiple partition keys.
You can't update an existing container to use a different unique key. In other words, after a container is created with a unique key policy, the policy can't be changed. To set a unique key for an existing container, create a new container with the unique key constraint.
Changing the partition key is not supported (see e.g. https://docs.microsoft.com/en-us/rest/api/cosmos-db/replace-a-collection). You would need to create a new collection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With