Amazon-Dynamodb [SOLVED]: DynamoDB: scan() with FilterExpression that combines multiple attributes

Amazon-Dynamodb [SOLVED]: DynamoDB: scan() with FilterExpression that combines multiple attributes

Home Forums Amazon Web Services Amazon Dynamodb Amazon-Dynamodb [SOLVED]: DynamoDB: scan() with FilterExpression that combines multiple attributes

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #247018

    Cloudy Point
    Keymaster

    QuestionQuestion

    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?

    #247019

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    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.

    Source: https://stackoverflow.com/questions/47924332/dynamodb-scan-with-filterexpression-that-combines-multiple-attributes
    Author: Abhaya Chauhan
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.