Scan with filter expressions lets you look through all items in a table but only keep the ones that match certain rules. It helps find specific data without reading everything.
0
0
Scan with filter expressions in DynamoDB
Introduction
You want to find all customers from a certain city in your database.
You need to get all products that cost less than $20.
You want to list all orders placed after a specific date.
You want to see all employees with a job title of 'Manager'.
Syntax
DynamoDB
Scan( TableName='YourTableName', FilterExpression='attribute_name = :value', ExpressionAttributeValues={ ':value': {'S': 'YourValue'} } )
FilterExpression is a condition that filters items after scanning.
ExpressionAttributeValues holds the values used in the filter.
Examples
Finds all products with price less than 20.
DynamoDB
Scan( TableName='Products', FilterExpression='Price < :maxPrice', ExpressionAttributeValues={ ':maxPrice': {'N': '20'} } )
Finds all employees who are Managers.
DynamoDB
Scan( TableName='Employees', FilterExpression='JobTitle = :title', ExpressionAttributeValues={ ':title': {'S': 'Manager'} } )
Sample Program
This scans the 'Books' table and returns only books where the Author is 'Alice'. It prints each matching item.
DynamoDB
import boto3 client = boto3.client('dynamodb') response = client.scan( TableName='Books', FilterExpression='Author = :author', ExpressionAttributeValues={ ':author': {'S': 'Alice'} } ) items = response.get('Items', []) for item in items: print(item)
OutputSuccess
Important Notes
Scan reads the whole table, so it can be slow for big tables.
FilterExpression filters results after scanning, so it doesn't reduce read capacity used.
Use Query instead if you can filter by primary key for better speed.
Summary
Scan with filter expressions helps find items matching conditions in a table.
It scans all items but only returns those that meet the filter.
Good for simple searches but can be slow on large tables.