DynamoDB CLI Queries
Queries:
Key condition expression: retrieve items by username
Here we get all items for a particular user. The username
field is not a primary key, but because it has an index, we can use a key condition expression:
aws dynamodb query \
--table-name MyTable \
--key-condition-expression "username = :username" \
--expression-attribute-values '{":username": {"S": "jsdanny"}}' \
--index-name username-index
2
3
4
5
Key condition and filter expression combined
Here we add a filter expression to our previous query:
aws dynamodb query \
--table-name MyTable \
--key-condition-expression "username = :username" \
--expression-attribute-values '{":username": {"S": "pydanny"}}' \
--index-name username-index \
--filter-expression "attribute_exists(shared_usernames)"
2
3
4
5
6
TIP
If you don't have a key condition or wish to filter on something without a primary key or index, do a scan
instead of a query
. It will retrieve all table items. Then they will be filtered based on your filter expression.
Filter results of a scan
Here we are filtering for items that have been shared with a particular user:
aws dynamodb scan \
--table-name RecordTable \
--filter-expression "contains(shared_usernames,:username)" \
--expression-attribute-values '{":username": {"S": "zif"}}'
2
3
4
Since the shared_usernames
field is not indexed, we cannot use a key condition expression. We have to use a scan and get all items, then filter those for our desired condition.
WARNING
If you are frequently filtering on a particular field, consider adding an index on that field and using key condition expressions instead.
Copyright © 2018 Daniel and Audrey Roy Greenfeld.
Site Map