ocind
ocind

Reputation: 159

Multiple AND and OR conditons for Filter Expression with DynamoDbMapper

I would like to create a filter expression with DynamoDbMapper such as Condition A AND Condition B AND (Condition C OR Condition D OR Condition E). Seems like DynamoDbMapper could only accept 1 conditional operator only (either all conditions AND operator / all conditions with OR operator).

Is it possible to achieve this with DynamoDbMapper?

Upvotes: 0

Views: 1728

Answers (1)

Matthew Pope
Matthew Pope

Reputation: 7669

This is definitely possible. Here is an example for a scan, though it would also work for a query. This example will find all books that are less than $10 and all CDs that are more that $10.

Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
eav.put(":val1", new AttributeValue().withN(10));
eav.put(":val2", new AttributeValue().withS("Book"));
eav.put(":val3", new AttributeValue().withS("CD"));

DynamoDBScanExpression scanExpression = new DynamoDBScanExpression()
    .withFilterExpression("Price < :val1 and ProductCategory = :val2 or Price > :val1 and ProductCategory = :val3")
    .withExpressionAttributeValues(eav);

List<Book> scanResult = mapper.scan(Book.class, scanExpression);

You can verify this by checking out the Syntax for Condition Expressions. Also, here is the relevant javadoc for DynamoDBQueryExpression and DynamoDBScanExpression.

Upvotes: 1

Related Questions