GrandPa
GrandPa

Reputation: 504

Using Amazon/AWS DynamodB scanfilter with both OR and AND conditions in java

I am using dynamoDB and using java. I have a usecase where i have to scan using filter expression.I have table named Order and with many fields. For my scan expression i need three fields named "field1","field2","field3".

Basically my query looks like

select * from Order where (field1 is  null OR field2 is null) AND (field3 != "Test")

I am able to form scan expression only with OR , but i am stuck on how to add AND here. Looks like we can only use one operator. Do we have any other ways to achieve this.

Following is the code snippet using only OR condition. I want to add AND condition to following snippet.

        final HashMap<String, Condition> scanFilter = new HashMap<>();
        scanFilter.put("field1", new Condition().withComparisonOperator(ComparisonOperator.NULL));
        scanFilter.put("field2", new Condition().withComparisonOperator(ComparisonOperator.NULL));
        final DynamoDBScanExpression expression = new DynamoDBScanExpression()
                .withConsistentRead(false)
                .withConditionalOperator(ConditionalOperator.OR)
                .withScanFilter(scanFilter);
        final ScanResultPage<SnapshotByGrantee> scanResult = scanPage(Order.class, expression, exclusiveStartKey, limit);
       

Upvotes: 0

Views: 1261

Answers (1)

GrandPa
GrandPa

Reputation: 504

This is what i have finally done to make it work!!.

        Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
        expressionAttributeValues.put("col1", new AttributeValue().withS("value1"));
        expressionAttributeValues.put(":col3", new AttributeValue().withS("value2"));
        expressionAttributeValues.put(":col3", new AttributeValue().withS("value3"));
        Map<String, String> expressionAttributeNames= new HashMap<>();
        expressionAttributeNames.put("#col4", "col4");
        final DynamoDBScanExpression expression = new DynamoDBScanExpression()
                .withConsistentRead(false)
                .withFilterExpression("(attribute_not_exists("+something1+")  or  attribute_not_exists("+something2+") ) AND (not  (#col4 in (:test1, :test2, :test3 ))) ")
                .withExpressionAttributeNames(expressionAttributeNames)
                .withExpressionAttributeValues(expressionAttributeValues);

Upvotes: 1

Related Questions