Jorge Guerreiro
Jorge Guerreiro

Reputation: 847

Query DynamoDB using Between operator

I have a DynamoDb table with the following structure

key       reference          value
1234      3214#0012345       xxxx
1234      3314#0003455       xxxx
1234      3214#0004231       xxxx

The value of reference, is a concatenation like so:

${accountId}#${idOfItem}

I'm trying to query the table to retrieve value but only where the account ID's match my query. So far I tried this

public Promise<List<Value>> getFiltersByUser(Key key, AccountId accountId) {
        final QuerySpec query = new QuerySpec();
        Map<String, Object> valueMap = new HashMap<>();
        Map<String, String> keyMap = new HashMap<>();

        keyMap.put("#key", PARTITION_KEY_NAME);
        keyMap.put("#reference", RANGE_KEY_NAME);
        valueMap.put(":key", key);
        valueMap.put(":startReference", createFilterRef("0000000", accountId));
        valueMap.put(":endReference", createFilterRef("9999999", accountId));
//createFilterRef simple creates the reference by concatening the two values with hashtag
        query.withKeyConditionExpression("#key = :key AND #filterRef BETWEEN :startFilterRef AND :endReference")
                .withNameMap(keyMap)
                .withValueMap(valueMap);

        return Blocking.get(() -> {
            final ItemCollection<QueryOutcome> out = table.query(query);
            return newArrayList(out)
                    .stream()
                    .map(this::createValueFromItem)//we can disregard this as it converts the results received to a different structure
                    .collect(Collectors.toList());
        });
    }

The problem I'm having currently is that my condition expression seems to not care about my BETWEEN operator, or I'm doing something wrong, and retrieves all the values instead.

Any advice on how to reach my goal?

Upvotes: 0

Views: 348

Answers (0)

Related Questions