DEV Community

Abhishek Gupta for AWS

Posted on • Originally published at community.aws

2

[20 Days of DynamoDB] Day 10 - Query API with Filter Expression

Posted: 19/Jan/2024

With the DynamoDB Query API, you can use Filter Expressions to discard specific query results based on a criteria. Note that the filter expression is applied after a Query finishes, but before the results are returned. Thus, it has no impact on the RCUs (read capacity unit) consumed by the query.

Here is an example that filters out forum discussion threads that have less than a specific number of views:

    resp, err := client.Query(context.Background(), &dynamodb.QueryInput{
        TableName:              aws.String(tableName),
        KeyConditionExpression: aws.String("ForumName = :name"),
        FilterExpression:       aws.String("#v >= :num"),
        ExpressionAttributeNames: map[string]string{
            "#v": "Views",
        },
        ExpressionAttributeValues: map[string]types.AttributeValue{
            ":name": &types.AttributeValueMemberS{Value: forumName},
            ":num":  &types.AttributeValueMemberN{Value: numViews},
        },
    })
Enter fullscreen mode Exit fullscreen mode

Recommended reading: Filter Expressions

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay