Fastidious
Fastidious

Reputation: 1319

How can you query begins_with for primary keys in DynamoDB?

What's the best way to filter results with the begins_with method for boto3.dynamodb.condition.Key?

import boto3
from boto3.dynamodb.conditions import Key

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('MyTable')
response = table.query(KeyConditionExpression=Key('myPrimaryKey').begins_with('americas'))

The results, although conditions supports begins_with, is the query is not supported. Can you not use begins_with on primary or sort keys?

Upvotes: 1

Views: 11276

Answers (1)

Seth Geoghegan
Seth Geoghegan

Reputation: 5747

Primary keys in DynamoDB can be either simple or composite.

Simple primary keys are consist of a partition key and no sort key.

Composite primary keys have both a partition key and a sort key.

You can only use begins_with on the sort key, not the partition key.

Upvotes: 8

Related Questions