As the title said, I can’t query for all sort key in primary key in DynamoDB. It just got difference error whatever I tried some difference approach.
Also, my sort keys don’t start with the same string, so BEGIN_WITH is unusable.
I have already tried some difference command, like
const command = new QueryCommand({
TableName: "MyTable",
KeyConditionExpression: 'PK = :pk,
ExpressionAttributeValues: {
":pk": tempPK,
},
});
It got this error "One or more parameter values were invalid: Condition parameter type does not match schema type". It seems like maybe the ‘KeyConditionExpression’ require both primary and sort key.
I have also tried using "attribute_exists"
const command = new QueryCommand({
TableName: "MyTable",
KeyConditionExpression: 'PK = :partitionKey AND attribute_exists(SK)',
ExpressionAttributeValues: {
':partitionKey': tempPK,
},
});
It got this error "Invalid operator used in KeyConditionExpression: attribute_exists". As it said, attribute_exists doesn’t vaild.
So, I came up with this. By using greater that sign, it should work.
const command = new QueryCommand({
TableName: 'MyTable',
KeyConditionExpression: 'PK = :partitionKey AND SK > :minSortKey',
ExpressionAttributeValues: {
':partitionKey': tempPK,
':minSortKey': '',
},
});
Yet it still error, got "Query key condition not supported."
I began just two weeks ago, and I would greatly appreciate any assistance or guidance. Thank you!
2
Answers
My assumption here is that you are passing the wrong format to your client. You never show how you create the client or the value of
tempPK
.I wrote a blog on the different clients which you can use and the parameters you should pass, being unfamiliar with DynamoDB I recommend you use the high level client. Take a read of the blog, it will most likely help you understand your issue.
This code will return all of the items which match tempPK partition key.
Your first example appears like the correct approach. I have a similar call where I’m getting all records using only the primary key. When I compare my code with yours, I do see a couple of differences though.
Here is an updated version based on the query I have in my code. Hopefully this helps.
I’m not sure if the value needs to be in quotes or not. It does for me. So try both of these. "S" here assumes your primary key is a string. If it’s a number, replace with "N".
"S": tempPK
and"S": "tempPK"
One final tip. DynamoDB can be particular about double/single quotes. Especially when specifying indexes in the query. Keep that in mind when building your queries.