0
0
DynamoDBquery~30 mins

Filter expressions in DynamoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Filter Items in DynamoDB Using Filter Expressions
📖 Scenario: You manage a DynamoDB table that stores information about books in a library. You want to find books that are currently available for borrowing.
🎯 Goal: Build a DynamoDB query that uses a filter expression to retrieve only the books where the available attribute is true.
📋 What You'll Learn
Create a dictionary called table representing the DynamoDB table with sample book items.
Add a filter expression variable called filter_expression to select books where available is true.
Write a query using the scan method with the filter expression to get only available books.
Complete the query setup by including the filter expression in the scan parameters.
💡 Why This Matters
🌍 Real World
Filtering data in DynamoDB tables is common when you want to retrieve only items that meet certain conditions, such as available products or active users.
💼 Career
Understanding filter expressions is essential for backend developers and database administrators working with AWS DynamoDB to optimize data retrieval and reduce costs.
Progress0 / 4 steps
1
Create the DynamoDB table data
Create a dictionary called table with a key Items containing a list of three book dictionaries. Each book dictionary must have these exact keys and values: {'title': 'Book A', 'author': 'Author 1', 'available': True}, {'title': 'Book B', 'author': 'Author 2', 'available': False}, and {'title': 'Book C', 'author': 'Author 3', 'available': True}.
DynamoDB
Need a hint?

Use a dictionary with key 'Items' and a list of dictionaries for the books.

2
Define the filter expression
Create a variable called filter_expression and set it to the string 'available = :val' to filter books where the available attribute is true.
DynamoDB
Need a hint?

The filter expression is a string that compares available to a placeholder :val.

3
Write the scan query with filter expression
Create a dictionary called scan_params with keys FilterExpression set to filter_expression and ExpressionAttributeValues set to a dictionary with ':val' as True.
DynamoDB
Need a hint?

Use a dictionary with keys FilterExpression and ExpressionAttributeValues for the scan parameters.

4
Complete the scan query setup
Add a variable called result and set it to the result of calling table['Items'] filtered by the condition where the available attribute is True using the scan_params filter expression logic.
DynamoDB
Need a hint?

Use a list comprehension to filter table['Items'] where available equals True.