Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter a DyamoDB query to exclude values (DynamoDB, Javascript)

The Situation: I have created a DynamoDB which is accessed by a Lambda script. Accessing data with query and get are working fine - also on Global Secondary Indexes

The Challenge: For one use case I need to query for a Global Seconday Index and filter the result to exclude items when they have a certain value for another attribute

What I'm currently trying to do (sample code):

const paramsQueryAndFilter = {
        TableName: "Sample",

        //Search for items based on Global Secondary Index for Attribute 1
        IndexName: "Attribute_1-index", // is a Global Secondary Index
        KeyConditionExpression: "Attribute_1 = :Attribute_1",
        ExpressionAttributeValues: {
            ":Attribute_1": "70"
        },

        //Filter items 
        ExpressionAttributeNames: {
            "Attribute_2": "Attribute_2"
        },
        //Filter Attribute 2 to exclude value "excludeValue"
        FilterExpression: "Attribute_2 ne :excludedValue",
        ExpressionAttributeValues: {
            ":excludedValue": "false"
        }
}

What I'm looking for: I hope to get some idea on how to correctly filter out items based on Attribute_2.

like image 379
Geole Avatar asked Dec 03 '25 03:12

Geole


1 Answers

Use queryfilter in your above code

KeyConditionExpression: "Attribute_1 = :Attribute_1",
        ExpressionAttributeValues: {
            ":Attribute_1": "70"
        },

 QueryFilter: {
'<Attribute_1>': {
  ComparisonOperator: "EQ", /* required */
  AttributeValueList: [  "somevalue", "somevalue2"  ]
}

Refer to documentation: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#query-property

like image 192
Magnus Melwin Avatar answered Dec 07 '25 16:12

Magnus Melwin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!