Skip to content Skip to sidebar Skip to footer

Android Dynamodb Filtering Expression With String Set

I'm trying to filter the results of a query using a String Set. My String set is made up of countries. Basically, after performing the query, I want the filter to look at the conte

Solution 1:

One thing - a StringSet is a value type for a field of a DynamoDB item, and not something you can use to query for multiple things at once.

I looked around a bit and found some methods that would work elegantly with your design, but they're deprecated. What I would recommend is building up the query string yourself. For example...

String[] countries = {"Japan", "Vietnam", "Thailand"};

List<String> queries = newArrayList<>();
Map<String, AttributeValue> eav = newHashMap<>();

for (Integer i = 0; i < countries.length; i++) {
    String placeholder = ":val" + i;
    eav.put(placeholder, newAttributeValue().withS(countries[i]));
    queries.add("Place = " + placeholder);
}

String query = String.join(" or ", queries);

DynamoDBQueryExpression queryExpression = newDynamoDBQueryExpression()
    .withFilterExpression(query)
    .withExpressionAttributeValues(eav)
    .withHashKeyValues(someHashValue);

I haven't tested it, but this is the sort of thing you'd be looking at doing (unless there's a better way that I still haven't seen).

Post a Comment for "Android Dynamodb Filtering Expression With String Set"