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달 전355회 조회
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달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠