Antrikshy
Antrikshy

Reputation: 3106

Does DynamoDB have a default/no-op FilterExpression?

I want to scan a small DynamoDB table using Boto3, and optionally allow the caller of this code to indicate when optional conditions should apply to this scan.

It would be convenient in code to do this:

scan_kwargs = {'Select': 'ALL_ATTRIBUTES'}
filter_exp = ''  # WHAT DO HERE?
if condition1:
    filter_exp &= Attr('attribute').is_in(['blah', 'bloop'])
if condition2:
    filter_exp &= Attr('deleted').is_eq(True)
scan_kwargs.update({'FilterExpression': filter_exp})
response = table.scan(**scan_kwargs)

I'm trying to figure out if there is something I can set FilterExpression to be by default (indicated above in comment), for the scan to work when there are no filters to be applied.

As far as I can tell, this is impossible.

Upvotes: 1

Views: 908

Answers (1)

Antrikshy
Antrikshy

Reputation: 3106

Bit scrappy, but I figured I could do...

filter_exp = Attr('pKey').exists()

... where pKey is the primary key or another mandatory field in the table.

It's definitely not the "correct" solution, but might be the most reliable no-op filter that I can build upon conditionally.

If I am right in interpreting the documentation, a scan operation consumes the same amount of read capacity regardless of any filter expressions.

Upvotes: 2

Related Questions