3
votes

I am trying to run a DynamoDB query that says I want items that do not begin with a particular value. I cannot seem to find a way to do this.

I have tried the following 4 ways of evaluating, and none of them work. Each one gives me an invalid operator error.

My KeyConditionExpression(s) that I have tried look like this:

!begins_with(gameScoreId, :scoreScore) AND !begins_with(gameScoreId, :scoreLevel) AND userId = :userId

<>begins_with(gameScoreId, :scoreScore) AND <>begins_with(gameScoreId, :scoreLevel) AND userId = :userId

NOT begins_with(gameScoreId, :scoreScore) AND NOT begins_with(gameScoreId, :scoreLevel) AND userId = :userId

begins_with(gameScoreId, :scoreScore) = false AND begins_with(gameScoreId, :scoreLevel) = false AND userId = :userId

If I remove the not operators, I get this error:

KeyConditionExpressions must only contain one condition per key

Is there a way to do this in dynamodb?

2
Is userId the partition key and gameScoreId the sort key?jarmod
Yeah that's correctGet Off My Lawn

2 Answers

1
votes

It looks like key condition expression does not support NOT begins_with(x). This might be because the result set is not contiguous (it's the items before x, aggregated with those after x).

Some possible solutions are:

  1. make the gameScoreId a non-key attribute (or replicate it into a new non-key attribute), then you can query on the userId and filter on the gameScoreId (you cannot filter on key attributes), or
  2. scan the table, in which case you can apply the filter expression that you want to use (obviously you will have performance concerns with very large tables)
-1
votes
        .withFilterExpression("NOT begins_with(hashKey, :hKey)")
        .withExpressionAttributeValues(ImmutableMap.of(
                ":hKey", new AttributeValue().withS("somePrefix:")))

This seems to work for me when using filter expressions. I think your problem is more with using more conditions on one key.