16

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.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Mike
  • 205
  • 2
  • 10
  • I believe you cannot. In my experience I ended up creating a new collection when wanting to change the partition key. You may find more information here: https://learn.microsoft.com/en-us/azure/cosmos-db/partition-data#designing-for-partitioning – brudert Jul 22 '17 at 00:12

1 Answers1

9

Changing the partition key is not supported (see e.g. https://learn.microsoft.com/en-us/rest/api/cosmos-db/replace-a-collection). You would need to create a new collection.

quervernetzt
  • 10,311
  • 6
  • 32
  • 51
  • 5
    This is such an important point that NOWHERE, absolutely NOWHERE has this been mentioned on all online docs. At least nowhere easy to locate on the MS website. The "Copy to another collection with a partition" process should be easier to do as IMO, nobody really knows the importance of a partition unless they have to and by then, its too late. – Dilnoor Singh Mar 19 '21 at 09:33
  • There is a blog post that describes an azure-change-partitionkey workflow: https://devblogs.microsoft.com/cosmosdb/how-to-change-your-partition-key/ – Reinhard Feb 07 '22 at 09:39