Untitled

 avatar
unknown
plain_text
23 days ago
1.4 kB
4
Indexable
import boto3
from boto3.dynamodb.conditions import Attr

def scan_dynamodb_table_less_than(table_name, field_name, field_value):
    """
    Scan a DynamoDB table and filter results based on a less than condition.

    :param table_name: Name of the DynamoDB table.
    :param field_name: The field/attribute to filter on.
    :param field_value: The value of the field to be less than.
    :return: List of matching items.
    """
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(table_name)

    # Create a filter expression to match "less than"
    filter_expression = Attr(field_name).lt(field_value)

    # Perform a table scan with the filter expression
    response = table.scan(
        FilterExpression=filter_expression
    )
    items = response.get('Items', [])

    # Check if there are more items to scan (pagination)
    while 'LastEvaluatedKey' in response:
        response = table.scan(
            FilterExpression=filter_expression,
            ExclusiveStartKey=response['LastEvaluatedKey']
        )
        items.extend(response.get('Items', []))

    return items

# Usage example
if __name__ == "__main__":
    table_name = "YourTableName"
    field_name = "YourFieldName"
    field_value = 100  # Example value for comparison (e.g., 100)

    matches = scan_dynamodb_table_less_than(table_name, field_name, field_value)
    print(f"Filtered items: {matches}")
Leave a Comment