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 個月前檢視次數 347 次
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 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南