The hierarchical partition key is constructed by combining multiple values into a single partition key path. This is defined in your container settings.
For example, if your partition key path is /level1/level2
, you would combine level1
and level2
values into a single partition key when inserting or querying data.
In your case with level1
value as '123'
and level2
value as 'ABC'
, your partition key for these levels combined would typically look something like 123-ABC
if you choose to use a hyphen (-
) as a separator.
However, the actual format of your hierarchical partition key depends on how you've chosen to format these keys when you inserted the data into your container.
When you want to query or make operations on items with hierarchical partition keys with the Python SDK, you should specify the partition key by using the combined format.
partition_key = '123-ABC' # Assuming '-' is used as a separator
query = "SELECT * FROM c WHERE c.someField = 'someValue'"
items = cosmos.query_items(query=query, enable_cross_partition_query=True, partition_key=partition_key)
If the case is updating a document partially, you define the partition key in the same way:
partition_key = '123-ABC' # Use the combined partition key value
id = '<your-document-id>'
operations = [
{
'op': 'add',
'path': '/newField',
'value': 'newValue'
}
# Other operations as needed
]
cosmos.patch_item(item=id, partition_key=partition_key, patch_operations=operations)
The separator I used is just an example. You need to use whatever format you've chosen when you designed your data model and inserted data into Cosmos DB and make sure that the format of the partition_key
you use in your queries and operations matches exactly with how it's stored in Cosmos DB, including the order of the hierarchical levels and the separator.