'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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1