Flurin
Flurin

Reputation: 679

DynamoDB: scan() with FilterExpression that combines multiple attributes

Items in my DynamoDB table have the following format:

{
  'id': 1,
  'last_check': 1234556,
  'check_interval': 100,
  ....
}

Now I'd like to scan the table and find all items where last_check + check_interval is less than some given value last_check_time. I did not find a way to create a FilterExpression that combines two attributes so I'm currently doing it the following way:

last_check_time = time.time()
response = status_table.scan(
    ProjectionExpression='id, last_check, check_interval',
    FilterExpression = Attr('last_check').lt(last_check_time)
)
# manually filter the items and only preserve those with last_check + check_interval < last_check_time:
for item in response['Items']:
    if item['last_check'] + item['check_interval'] < last_check_time:
        # This is one of the items I'm interested in!
        ....
    else:
        # Not interested in this item. And I'd prefer to let DynamoDB filter this out.
        continue

Is there a way to let DynamoDB do the filtering and therefore make the for loop in the example above obsolete?

Upvotes: 0

Views: 2080

Answers (1)

Abhaya Chauhan
Abhaya Chauhan

Reputation: 1201

Unfortunately it is currently not possible to request DynamoDB to perform a filtered calculation for you, but you could create another attribute which is the sum of the two attributes, and you have a couple of approaches to achieve that;

  1. Potentially compute an additional attribute (last_check + check_interval) in code when writing the item to DynamoDB.

  2. Use DynamoDB Triggers to create an additional attribute (last_check + check_interval)

You can use either option to create a new attribute on the item to filter on.

Upvotes: 2

Related Questions