2

How can I extract a list of all the partition keys in a collection? Assume I have the Collection Link of the cosmos DB.

I am trying something like this:

foreach (var id in client.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri("db12", "coll12"),
    "SELECT DISTINCT c.Partitionkey FROM c", queryOptions))
            {
                Console.WriteLine("\tRead------------------- {0} from SQL", id);
            }

this gives empty result.

das
  • 201
  • 1
  • 3
  • 10
  • duplicate of [Get all the Partition Keys in Azure Cosmos DB collection](https://stackoverflow.com/questions/45093202/get-all-the-partition-keys-in-azure-cosmos-db-collection) – TJ Galama Oct 03 '19 at 14:03

2 Answers2

1

Here are some ideas to try to troubleshoot this issue. It's possible that:

  • The collection is empty;
  • There are no documents that contain PartitionKey name;
  • That you meant to query the document partition key, which may be different than /PartitionKey for example;

--- Querying and Inspecting Documents ---

You can query the documents in a collection in the Azure Portal, navigate to the CosmosDB/SQL Account > Data Explorer > {Database Name} > {Collection Name} > New SQL Query. Then, run query experiments there and view all (paged) documents as well.

--- Find the Partition Key ---

You can double check the collection partition key in the Azure Portal as well, navigate to the CosmosDB/SQL Account > Data Explorer > {Database Name} > {Collection Name} > Scale & Settings and check the what is defined on the Partition Key field (e.g. /customerid, /name, etc.).

As an example, considering the partition key is defined as /customerid, the query will look like:

SELECT c.customerid FROM c

Or the following query in case /customerid is not the partition key and may have duplicates:

SELECT DISTINCT c.customerid FROM c
Evandro de Paula
  • 2,532
  • 2
  • 18
  • 27
1

You just need to modify the Partitionkey to the field name of your partition key.

For example, my partition key is name, so the query looks like :

select distinct c.name from c

My query result looks like :

enter image description here

It could also extract the "" and undefined.

Hope it helps you.

Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • I am looking for a generalized code to extract the list of partition keys of documents. For eg, here I am using Family Database and I have chosen 'LastName' as the partition key. I know if I directly do [ SELECT DISTINCT c.LastName FROM c ] it will give me the result I want. But there should be a way to extract the same result without explicitly specifying the Partition Key. – das Jun 08 '18 at 08:46
  • if u don't know which property stands for PartitionKey, u can find it out using `ReadDocumentCollectionAsync(collection)` from `Resource.PartitionKey` property. And than u can do distinct query by concrete property (partition key) – Olha Shumeliuk Jun 09 '18 at 20:07