0

I have a working DynamoDb query, but I'm trying to add an IN operator and can't figure out the syntax for the FilterExpression.

pages is a List<Integer> and I have an attribute in my table which is an Integer, so I'm doing something like this

attrValues.put(":pages", AttributeValue.fromL(pages.stream()
                     .map(i->AttributeValue.fromN(i.toString()))
                     .collect(Collectors.toList())));

String filterExpression = "in(originalPageNumber, :pages)"

final QueryRequest.Builder queryBuilder = QueryRequest.builder()
                .tableName(Parameters.addPrefix(TABLE_NAME))
                .keyConditionExpression(keyConditionExpression)
                .expressionAttributeNames(attrNameAlias)
                .expressionAttributeValues(attrValues)
                .filterExpression(filterExpression);

(this is not the complete code. Just the relevant parts)

But it doesn't like my syntax at all. I've tried originalPageNumber in :pages and other variations as well.

Update

Other things I tried

Tried setting :pages to

final String pagesStr = pages.stream().map(i -> i.toString()).collect(Collectors.joining(", ", "(", ")"));
final AttributeValue pagesAttr = AttributeValue.fromS(pagesStr);
attrValues.put(":pages", pagesAttr);

or AttributeValue(S=(1, 2))

I then got rid of :pages altogether and just set filterExpression to in(originalPageNumber, (1, 2)). Still no luck. Also tried originalPageNumber in (1, 2)

Peter Kronenberg
  • 878
  • 10
  • 32

1 Answers1

0

Try it like this:

String filterExpression = "originalPageNumber IN (:pages)"

:pages would need to be a comma separated list of values. Which should essentially map to:

String filterExpression = "originalPageNumber IN (1,2,3,7,9)"
Leeroy Hannigan
  • 11,409
  • 3
  • 14
  • 31
  • Thanks, I'll try that. Are you saying that :pages should be a literal string, and not an attributeValue object? – Peter Kronenberg Nov 09 '22 at 23:25
  • A literal string could possibly work, but I'm thinking more along the lines of parsing the values in the list to individual expressionAttributeValues like you can see in this JS example: https://stackoverflow.com/a/40294674/7909676 – Leeroy Hannigan Nov 09 '22 at 23:29
  • Tried your suggestions and still no luck. I updated my question with this additional information – Peter Kronenberg Nov 10 '22 at 02:36