Adding Partition key to the existing collection- Azure Cosmos DB - azure

Is there any way that we can add Partition Keys for the Collections we already have in Azure-Cosmos DB, or we need to drop them and create new collections with partition keys and import the data from the previous collections?
I tried googling a lot and checking the settings of the collection but nothing helped. if you could that would be great, thanks in advance.

Once created, a collections partition key definition cannot change. This means that you cannot add, remove or alter the partition key of a collection once created.
You can use the Cosmos DB change feed to migrate to a new collection with the appropriate partition key.

Related

Rsu same fro primary key and non-primary key - cosmos

I have a cosmos db container and currently it has just 100 documents. So when I query with Indexed id (primary key) or non-primary key, then the rsu remains the same.
So, as I have more data, the rsu's will change right ? Or does cosmos calculates based on more data and gives an average ?
I have id (primary key) as some unique ids and I am setting partition keys to be same as id. Because few times, i need to query based on id. But now there is a new requirement to query also on the basis of non-primary key. So, should I add that to be a partition key (again - unique ids) or add a secondary index ?
The data is not yet in production.
At small sizes the choices you are making here likely won't have any impact either way on RU/s costs. It's only when you want to scale your workload that design decisions you make will have an impact.
If you are new to Cosmos DB, I would recommend you watch this presentation on how to model and partition data in Cosmos DB. https://youtu.be/utdxvAhIlcY

How to update a Cosmos DB document partition key element?

I need to update a document changing the value of the element being used as the partition key. The documentation says that a document is uniquely identified by its id and partition key.
So, if I change the partition key will this always create new document?
Or, will it only create a new document if it is placed on another partition?
If a new document is always created then I think the safest way to update is
Create new document.
If successful, delete old document.
Failure to delete will result in duplicate data but at least the data is not lost.
If a new document is not always created, how can I identify the cases where a new document was created so that I can delete the old one? I don't want to delete anything without having the new one created first since there is no transactional way to do this.
Regards All.
Trying to update the partition key value will simply fail.
Trying to upsert the partition key value will create a new document with the same id in a different logical partition.
What the process should be is:
Keep the old document in memory
Delete the old document
Create the new document
If the later fails then recreate the old document
Cosmos DB doesn't support transactions so there is no way to do this otherwise, and you can't use a stored procedure as they only run against a single logical partition.

cosmosdb bulkdelete without partitionkey

Am trying to do a queried document bulk delete using storedprocedure on cosmosdb collection. I have used sample code from here .
https://github.com/Azure/azure-documentdb-js-server/blob/master/samples/stored-procedures/bulkDelete.js
When I try to execute the query, am forced to provide a partition key which I do not know. I want to execute a fan out delete query based on query criteria which do not include the partition key. What are other ways I can try and delete documents in bulk from a cosmosdb collection ?
If the collection the stored procedure is registered against is a
single-partition collection, then the transaction is scoped to all the
documents within the collection. If the collection is partitioned,
then stored procedures are executed in the transaction scope of a
single partition key. Each stored procedure execution must then
include a partition key value corresponding to the scope the
transaction must run under.
You could refer to the description above which mentioned here.
Surely, if your collection has been partitioned, you also need to offer partition key when you operate the collections or documents in it. More details from here.
So, base on your situation that you do not know the partition key, I suggest you set EnableCrossPartitionQuery to true in the FeedOptions when executing deletion.(has performance bottleneck)
Hope it helps you.

Azure Cosmos DB asking for partition key for stored procedure

I am using GUID Id as my partition key and I am facing problem when I am trying to run a stored procedure. To run a store procedure I need to provide partition key ans I am not sure what value should I provide in this case? Please assist.
If the collection the stored procedure is registered against is a
single-partition collection, then the transaction is scoped to all the
documents within the collection. If the collection is partitioned,
then stored procedures are executed in the transaction scope of a
single partition key. Each stored procedure execution must then
include a partition key value corresponding to the scope the
transaction must run under.
You could refer to the description above which mentioned here.
As #Rafat Sarosh said, GUID Id is not an appropriate partitioning key. Based on your situation , city may be more appropriate.You may need to adjust your database partitioning scheme because the partitioning key can not be deleted or modified after you have defined it.
I suggest you exporting your data to json file then import to a new collection which is partitioned by city via Azure Cosmos DB Data migration tool.
Hope it helps you.
Just for summary:
Issue:
Unable to provide specific partition key value when executing sql to query documents.
Solution:
1.Set EnableCrossPartitionQuery to true when executing query sql.(has performance bottleneck)
2.Consider setting a frequently queried field as a partitioning key.
Example your partition key is /id
and your cosmos document is
{
"id" : abcde
}
When store procedure run, you need to paste: abcde value
So if you want your store procedure running cross partition, it can't
Answer from cosmos team
https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/33550159-support-stored-procedure-execution-over-all-partit

Get all the Partition Keys in Azure Cosmos DB collection

I have recently started using Azure Cosmos DB in our project. For the reporting purpose, we need to get all the Partition Keys in the collection. I could not find any suitable API to achieve it.
UPDATE: According to Brian in the comments below, DISTINCT is now supported. Try something like:
SELECT DISTINCT c.partitionKey FROM c
Prior answer: Idea that could work but for one thing...
The only way to get the actual partition key values is to do a unique aggregate on that field.
You can directly hit the REST endpoint at https://{your endpoint domain}.documents.azure.com/dbs/{your collection's uri fragment}/pkranges to pull back the minInclusive and maxExclusive ranges for each partition but those are hash space ranges and I don't know how to convert those into partition key values nor do a fanout using the actual minInclusive hash.
Also, there is a slim possibility that the pkranges can change between the time you retrieve them and the time you go to do something with them.

Resources