das
das

Reputation: 221

Extract partition keys from cosmos DB

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.

Upvotes: 2

Views: 4285

Answers (2)

Evandro de Paula
Evandro de Paula

Reputation: 2642

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

Upvotes: 1

Jay Gong
Jay Gong

Reputation: 23782

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.

Upvotes: 1

Related Questions