Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have FilterExpression with multiple conditions in dynamodb

Tags:

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

like image 613
JithPS Avatar asked Jun 09 '16 07:06

JithPS


People also ask

Does Amazon DynamoDB support conditional operations?

Yes, like all the other database management systems, DynamoDB also supports all the conditional operators, User can specify a condition that is satisfied for a put, update, or delete operation to work on an item.

What is Filterexpression in DynamoDB?

A filter expression determines which items within the Query results should be returned to you. All of the other results are discarded. A filter expression is applied after a Query finishes, but before the results are returned.

What is condition expression in DynamoDB?

Conditional put The PutItem operation overwrites an item with the same key (if it exists). If you want to avoid this, use a condition expression. This allows the write to proceed only if the item in question does not already have the same key.


1 Answers

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") 
              )
like image 79
JithPS Avatar answered Sep 27 '22 23:09

JithPS