10

I'm trying to query a dynamodb table using the partition key and a sort key. The sort key is a unix date, so I want to request x partition key between these 2 dates on the sort. I am currently able to achieve this with a table scan, but I have to move this to a query for the speed benefit. I am unable to find any decent examples online of people using a partition key and sort key to query their table.

I have carefully read through this https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/#DynamoDB.Query and understand that my params must go within the KeyConditionExpression.

I have read through https://github.com/aws/aws-sdk-go/blob/master/service/dynamodb/expression/examples_test.go and understand it on the whole. But I just can't find the syntax for KeyConditionExpression

I'd have thought it was something like this:

keyCond := expression.Key("accountId").
    Equal(expression.Value(accountId)).
    And(expression.Key("sortKey").
    Between(expression.Value(fromDateDec), expression.Value(toDateDec)))

But this throws:

ValidationException: Invalid KeyConditionExpression: Incorrect operand type for operator or function; operator or function: BETWEEN, operand type: NULL

2
  • Could it be that the error message is accurate? I.e., that fromDateDec or toDateDec has a NULL value? Jun 11, 2019 at 15:41
  • Yes it was. toDate was supposed to be in64 rather than decimal.... i had read the error message incorrectly as thoguht the error was with me using the between function
    – rlou
    Jun 12, 2019 at 10:38

1 Answer 1

0

First you need KeyAnd to combine Hash Key and sort key condition.

// keyCondition represents the key condition where the partition key
// "TeamName" is equal to value "Wildcats" and sort key "Number" is equal
// to value 1
keyCondition := expression.KeyAnd(expression.Key("TeamName").Equal(expression.Value("Wildcats")), expression.Key("Number").Equal(expression.Value(1)))

Now instead equal condition you can replace with your between condition as follows

// keyCondition represents the boolean key condition of whether the value
// of the key "foo" is between values 5 and 10
keyCondition := expression.KeyBetween(expression.Key("foo"), expression.Value(5), expression.Value(10))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.