Skip to content

How can i query dynamodb objects that contain a nested object list?

0

I have a table of documents and i am trying to query them and get any documents that contain a nested object (NestedObjPropA in this example) that matches a specific string. Im really struggling to do it using a scan filterExpression. The table has its Partition key set to a timestamp.

Any suggestions are greatly appreciated.

{
	PropA: "",
	PropB: "",
	PropC: "",
	PropD: [
		{
			NestedObjPropA: "",
			NestedObjPropB: "",
			NestedObjPropC: ""
		},
		{
			NestedObjPropA: "",
			NestedObjPropB: "",
			NestedObjPropC: ""
		}
	],
}

asked 2 years ago2.3K views

2 Answers
1

When you want to see if a string exists in a list of objects, you unfortunately need to pass in the entire object, not just one of its values.

Typically when I model data like this in DynamoDB, I do so in a nested map, rather than a list of objects. This allows querying and updating easier, but may not be as friendly to handle client side.

AWS
EXPERT

answered 2 years ago

0

To query DynamoDB objects that contain a nested object list as described in your example, you'll need to use a combination of DynamoDB's scan operation and a filter expression. Since your table has a timestamp as the partition key and you're looking for items based on a nested attribute, a scan operation with a filter is the most appropriate approach.

Here's how you can structure your query:

  1. Use the Scan operation instead of Query, as you're not searching based on the primary key (timestamp).

  2. Apply a FilterExpression to check for the desired value in the nested object list.

  3. Use the 'contains' function in your FilterExpression to check if any item in the PropD list has the NestedObjPropA matching your specific string.

Your FilterExpression would look something like this:

contains(PropD, :nestedValue)

Where :nestedValue is an ExpressionAttributeValue that represents a map containing the NestedObjPropA with the specific string you're looking for.

Here's a pseudo-code example of how this might look:

scan({
    TableName: "YourTableName",
    FilterExpression: "contains(PropD, :nestedValue)",
    ExpressionAttributeValues: {
        ":nestedValue": {
            "NestedObjPropA": "YourSpecificString"
        }
    }
})

Keep in mind that scanning a large table can be expensive in terms of read capacity units. If you find yourself frequently querying based on this nested attribute, you might want to consider adding a global secondary index on NestedObjPropA to make these queries more efficient.

Also, remember that DynamoDB scans have a 1MB limit per operation. If your table is large, you may need to implement pagination to retrieve all matching items.

Lastly, if you're using a specific SDK or programming language, the exact syntax may vary, but the general approach remains the same.
Sources
Querying tables in DynamoDB - Amazon DynamoDB
Key condition expressions for the Query operation in DynamoDB - Amazon DynamoDB
Step 5: Query data in a DynamoDB table - Amazon DynamoDB

answered 2 years 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.