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!
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.
const command = new QueryCommand({
TableName: "MyTable",
KeyConditionExpression: 'PK = :pk, <--MISSING END QUOTE
ExpressionAttributeValues: {
":pk": tempPK, <--I HAVE DIFFERENT SYNTAX
},
});
Here is an updated version based on the query I have in my code. Hopefully this helps.
const command = new QueryCommand({
"TableName" : "MyTable",
"PrimaryKey": "PK",
"KeyConditionExpression": "PK = :pk",
"ExpressionAttributeValues": {
":pk": {
"S": tempPK
}
}
});
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.