25

I'm learning about how to filter results from a scan or query using Amazon's DynamoDB. I would expect an example filter to look like filter => name = Bob or some such. However, Amazon requires the use of a expression attribute such as filter => name = :person and then ExpressionAttributeValues => { ":person": {"S": "Bob"}}

This is confusing and hurts my head, why can't I use the simple name = Bob?


Official docs: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html#FilteringResults

Apparently working example near end: https://github.com/aws/aws-cli/issues/1073

1 Answer 1

29

This type of syntax follows an approach that is similar to prepared statements that are used in SQL systems. This was a design decision that the DynamoDB team at AWS made. One of the reasons is to allow fields that conflict with the lengthy list of reserved words (including 'name' that you were using in your example) that are defined by DynamoDB.

Avoiding reserved words is actually performed by using the ExpressionAttributeNames attribute and specifying the attribute names. You were referencing ExpressionAttributeValues which is where the list of values is specified. More information is available on the Using Placeholders for Attribute Names and Values documentation page.

Another motivation of this design is to separate the statement from the parameter names and values, similar to prepared statements in SQL as I've already mentioned. While this may seem odd at first it has the added benefit of effectively sanitizing your inputs in a NoSQL sense avoiding possible malicious or unintentional problems with your user input affecting the behavior of your request on the interaction with DynamoDB.

1
  • DynamoDB has a similar but optional naming convention for ExpressionAttributeName using '#' sign. I wonder why this was left optional while ExpressionAttributeValue syntax was made compulsory. I understand the naming conflict with reserved words but why not leave it optional, similar to ExpressionAttributeName? For example ``` KeyConditionExpression: "#myName = :myTag", ExpressionAttributeValues: { ":myTag": "Database" }, ExpressionAttributeNames: { "#myName": "Tag" } ``` Jan 26, 2020 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.