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
asked 8 months ago246 views
1 Answer
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
answered 8 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions