Filtering records using keys from an object array - DynamoD

0

I have an existing data structure in dynamodb where the data is in following format myPrimaryKey(ParitionKey) & History(attribute)

History: [{
        type: 'type1',
        data: {
            request: {
                addl_vars: {
                    key1: 'value1',
                    key2: 'value2'
                },
                body: {
                    param1: 'value1',
                    param2: 'value2'
                }
            }
        }
    }, {
        type: 'type2',
        data: {
            request: {
                addl_vars: {
                    key1: 'value1',
                    key2: 'value2'
                },
                body: {
                    param1: 'value1',
                    param2: 'value2'
                }
            }
        }
    }
]

I need to fetch all objects that satisfies the condition data.request.addl_vars.key1='value1'

I have used the below expression to fetch content but ended with errors.

{
    TableName: "table",
    FilterExpression: 'contains(**History[*]**, :index)',
    ExpressionAttributeValues: {
        ':index': {
            'data': {
                'request': {
                    'addl_vars': {
                        'key1': 'value1'
                    }
                }
            }
        }
    }
}

I tried replacing History[*] with History[0] and I'm able get only one object. The major concern here is that there could be numerous records that satisfy this condition, so can't use keys here.

Can you please help resolving this issue?

Patton
已提问 9 个月前350 查看次数
1 回答
0

Hi Patton,

To filter items in DynamoDB based on values within a nested list (like the History attribute in your example), you should use instead of contains in the FilterExpression a combination of projection expressions and filtering on the client side. Here's a high-level approach to achieve your goal:

  1. Perform a scan or query operation to retrieve all the items from your DynamoDB table.

  2. Use a projection expression to only retrieve the History attribute, as you are interested in filtering within this attribute.

  3. In your application code, filter the retrieved items based on the condition you mentioned: data.request.addl_vars.key1 = 'value1'.

Keep in mind that using client-side filtering can be less efficient than performing filtering on the server side using expressions, but in cases where complex nested structures are involved, it may be the most practical approach.

Hope this documentation also helps: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Attributes.html#Expressions.Attributes.NestedAttributes

Here you should find more examples on nested attributes: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html#Expressions.ExpressionAttributeNames.NestedAttributes

profile pictureAWS
已回答 9 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则

相关内容