R. Chady
R. Chady

Reputation: 1

Conditional query of sort key in DynamoDB not working?

I am building a trivial API using API Gateway through to DynamoDB. I am trying to do a conditional query against a sort key and I keep getting the message "Query key conditional not supported." However, everything I've read in the documents said this should indeed work.

I'm trying to do something like:

{
    "TableName": "testTable",
    "KeyConditionExpression": "requestTimeEpoch > :since",
    "ExpressionAttributeValues": {
        ":since": {
            #if($input.params('since') != "")
                "S": "$input.params('since')"
            #else
                "S": "1"
            #end
        }
    }
}

I read somewhere that someone had this issue when using a Number, so I tried switching it to a String instead. Either way, shouldn't this work?

(requestTimeEpoch is the sort/range key for the hash key)

Upvotes: 0

Views: 1231

Answers (1)

F_SO_K
F_SO_K

Reputation: 14799

You can't use the sort key on its own. Data is only sorted within its partition. So you need to specify a partition key (this has to be an equals operator) on your query.

If you don't want to use your partition key, you need to switch to a scan operation. A scan doesn't require any key conditions.

EDIT: So with a partition (hash) key it might be something like

{
    "TableName": "testTable",
    "KeyConditionExpression": "YourHash = :hash and requestTimeEpoch > :since",
    "ExpressionAttributeValues": {
        ":since": {
            #if($input.params('since') != "")
                "S": "$input.params('since')"
            #else
                "S": "1"
            #end
        ":hash" : {S: 'yourHashId'}
        }
    }
}

and a scan

var params = {
    TableName: "testTable",
    FilterExpression: "#since > :time",
    ExpressionAttributeNames: {
        "#since": "time",
    },
    ExpressionAttributeValues: {
         ":time": 2017,
    }
};

Upvotes: 1

Related Questions