1
votes

This may seem like a dumb question but: As I understand, a hash and range key combined act as a way to uniquely identify an item in a table. However, every example I've seen of database, as well as the query documentation shows querying with the hash and/or range key to return multiple values. How do I use the range and hash to query (or some other operation) for a single item?

1

1 Answers

2
votes

From the DynamoDB client documentation here, when querying a table, querying returns ALL items which have the same hash key. If the table has only a hash type primary key (as opposed to a hash+range type primary key), then the result will contain zero or one item. If the table has hash + range primary key, then the result can contain zero or more items. In the hash + range primary key case, if a range key comparison operator is provided (equals, between, etc.) then the query result can be narrowed to fewer records. Providing an equals comparison operator for the range query will return zero or one records.

So, returning one item is really only a special case in returning a list of items.

However, if you feel that using an API to fetch a single item helps improve semantics / readability of your code - you can use a variant of getItem.