Reputation: 1506
I'm still learning DynamoDB and I would like to get a single item from a table based on two unique attributes (tconst and primaryTitle) in the table. Both of those attributes have unique values for each row. The primaryKey is tconst and the sortKey is primaryTitle
I thought that I could do something like below:
aws dynamodb query \
--endpoint-url http://localhost:8000 \
--table-name title \
--key-condition-expression "tconst = :tconst" and "primaryTitle = :primaryTitle" \
--expression-attribute-values '{
":tconst":{"S":"xxxx"},
":primaryTitle":{"S":"something"}
}'
"Item": {
"tconst": {
"S": "xxxx"
},
"titleType": {
"S": "xxxx"
},
"primaryTitle": {
"S": "something"
},
"originalTitle": {
"S": "Travel Daze"
},
"isAdult": {
"S": "0"
},
"startYear": {
"S": "2019"
},
"endYear": {
"S": "\\N"
},
"runtimeMinutes": {
"S": "\\N"
},
"genres": {
"S": "\\N"
}
},
"Item": {
"tconst": {
"S": "yyyy"
},
"titleType": {
"S": "yyyy"
},
"primaryTitle": {
"S": "Travel Daze"
},
"originalTitle": {
"S": "Travel Daze"
},
"isAdult": {
"S": "0"
},
"startYear": {
"S": "2019"
},
"endYear": {
"S": "\\N"
},
"runtimeMinutes": {
"S": "\\N"
},
"genres": {
"S": "\\N"
}
}
Upvotes: 0
Views: 6031
Reputation: 13108
If you know the primary key (i.e. partition and sort key in your case) for your item and only want to fetch one anyway, the GetItem
API call does exactly that. Query
allows you to filter inside of an item collection (all items that share the same partition key) - it has additional functionality, which you may not need here.
The GetItem operation returns a set of attributes for the item with the given primary key. If there is no matching item, GetItem does not return any data and there will be no Item element in the response.
— docs
If you want to keep using the Query - you can limit the number of results using the aptly names Limit-Parameter.
Upvotes: 3