- Newest
- Most votes
- Most comments
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.
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:
-
Use the Scan operation instead of Query, as you're not searching based on the primary key (timestamp).
-
Apply a FilterExpression to check for the desired value in the nested object list.
-
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
