- Le plus récent
- Le plus de votes
- La plupart des commentaires
When working with composite sort keys in DynamoDB, you can use the begins_with function in your key condition expressions to query at different levels of the hierarchy, not the contains function.
The hierarchical design with composite sort keys allows you to query at any level of the hierarchy by using the begins_with operator on the sort key. This works because the values are stored in a predictable format with delimiters (like "#").
For example, if your sort key follows the pattern [country]#[region]#[state]#[county]#[city]#[neighborhood], you can query:
- For all items in a specific country:
KeyConditionExpression: "partitionKey = :pk AND begins_with(sortKey, 'USA#')"
- For all items in a specific region within a country:
KeyConditionExpression: "partitionKey = :pk AND begins_with(sortKey, 'USA#Northeast#')"
- For all items in a specific state:
KeyConditionExpression: "partitionKey = :pk AND begins_with(sortKey, 'USA#Northeast#NewYork#')"
And so on for more specific levels.
In Python using Boto3, this would look like:
key_condition = Key('partitionKey').eq(partition_key_value) & Key('sortKey').begins_with('USA#Northeast#NewYork#') response = table.query(KeyConditionExpression=key_condition)
The important points to remember:
- You must always specify the partition key in your query
- Use
begins_with(notcontains) for hierarchical queries - Include all higher levels of the hierarchy and the delimiter when querying a specific level
- The query is efficient because it uses the sort key index
This approach allows you to model one-to-many relationships and query at any level of the hierarchy efficiently.
Sources
Community | 20 Days of DynamoDB
Query DynamoDB tables using date and time patterns with an AWS SDK - AWS SDK Code Examples
Query a DynamoDB table using a date range in the sort key with an AWS SDK - Amazon DynamoDB
Query DynamoDB tables using date and time patterns with an AWS SDK - Amazon DynamoDB
DynamoDB PartiQL vs. Scan | AWS re:Post
Great question — this is a common point of confusion when starting with composite sort keys. The key thing to remember is that DynamoDB doesn’t support contains() on sort keys for efficient lookups — instead, you leverage begins_with() or key equality.
For your example:
PK = "USA" SK = "USA#CA#OrangeCounty#Irvine"
You can query by state like this:
KeyConditionExpression = "PK = :country AND begins_with(SK, :state)" ExpressionAttributeValues = { ":country": "USA", ":state": "USA#CA" }
This will return all items where the sort key starts with USA#CA, effectively giving you all counties/cities in California.
The main advantage of composite sort keys is that you can “slice” the hierarchy at any level — country, region, state, etc. — as long as you query with a proper prefix using begins_with().
Contenus pertinents
- demandé il y a 8 mois
- demandé il y a 8 mois
- demandé il y a 3 ans

Yes that's what I thought - I was just missing the example in the documentation.
Honestly every time I read this section I am a bit confused and I am suddenly happy because it sounds to me like something which would solve lot of my head aches. But I understand how the hierarchy works, I just always mentally omit "hierarchy" word from the sentence I guess.
This just means that you always need to have all the information of the hierarchy when you try to query the subset of data.
Thanks for the explanation