JithPS
JithPS

Reputation: 1217

How to have FilterExpression with multiple conditions in dynamodb

I am trying to do table scan on dynamodb Below is the code which is in javascript

var params = {
    TableName: 'Contacts',
    FilterExpression: 'begins_with(CustomerName,:value)OR begins_with(CustomerName,:val) ', 
    ExpressionAttributeValues: { 
        ':value': {'S':'S'},
        ':val':{'S':'E'},
      },
    Select: 'ALL_ATTRIBUTES', 
 };

 dynamodb.scan(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response
});

But I couldn't try the same using botot3.

Below is what I could achieve so far

response = table.scan(
                  Select= 'ALL_ATTRIBUTES',
                  FilterExpression=Attr('CustomerName').begins_with("S") 
                  )

I couldn't understand how to add the OR condition. If I add, it shows error

Upvotes: 24

Views: 58726

Answers (2)

Oirad
Oirad

Reputation: 51

You can create a string compare= ["a = b", "c begins_with val'] and then join them with ' and '.join(compare)

creating for you the filter expression 'a = b and c begins_with val'

Upvotes: 1

JithPS
JithPS

Reputation: 1217

For AND '&' is used and for OR '|' is used

  response = table.scan(
              Select= 'ALL_ATTRIBUTES',
              FilterExpression=Attr('CustomerName').begins_with("S") | Attr('CustomerName').begins_with("S") 
              )

Upvotes: 55

Related Questions