What is the proper syntax to query database entries whose sort key starts with a specific string?
I believe it’s something along the lines of
JavaScript
x
11
11
1
const query_params = {
2
TableName: 'my_table',
3
Key: {
4
my_primary_key: 'a_primary_key_value',
5
},
6
FilterExpression: "my_sort_key begins_with :string",
7
ExpressionAttributeValues: {
8
":string": "starts_with_substring"
9
}
10
};
11
Followed by a dynamoDb.get(query_params, ...
, but that is not quite right. I am getting an ValidationException: The provided key element does not match the schema
error.
Advertisement
Answer
According to the SDK query documentation, your query params should look like this
JavaScript
1
13
13
1
{
2
TableName: "my-table",
3
KeyConditionExpression: "#pk= :pk And begins_with(#sk, :sk)",
4
ExpressionAttributeValues: {
5
":pk": "a_primary_key_value",
6
":sk": "starts_with_substring"
7
},
8
ExpressionAttributeNames: {
9
"#pk": "my_primary_key",
10
"#sk": "my_sort_key"
11
}
12
}
13
You’ll also need to change dynamoDb.get()
to dynamoDb.query()
.