Reputation: 463
I have one dynamodb table and one record is like belwo:
{
"Items": [
{
"toObjectId": {
"S": "5678"
},
"keyValue": {
"S": "7890"
},
"aTypeId": {
"S": "test"
},
"aws:rep:deleting": {
"BOOL": false
},
"currentAt": {
"N": "1582476260000"
},
"keyTypeId": {
"S": "test2"
},
"aId": {
"S": "1234"
},
"aTypeId_keyTypeId_keyValue_currentAt": {
"S": "test_test2_7890_1582476260000"
},
"fromObjectId": {
"S": "12345"
},
}
],
"Count": 2562,
"ScannedCount": 2562,
"ConsumedCapacity": null
}
How can I write one aws dynamodb scan/query filter with aws cli to just get aTypeId and aId when aTypeId is "test"? And
Primary partition key is aId (String)
Primary sort key is aTypeId_keyTypeId_keyValue_currentAt (String)
I have tried below but no lucky with it
aws dynamodb query \
--table-name test \
--key-condition-expression "aTypeId = :aTypeId" \
--expression-attribute-values '{
":aTypeId": { "S": "test" }
}'
Upvotes: 1
Views: 14351
Reputation: 1058
Since aId
is a Primary Key and you are trying to search for it, you can only scan through the table (which is might be very expensive operation). Try below command to scan the item
aws dynamodb scan \
--table-name test \
--projection-expression "aTypeId, aId" \
--filter-expression "aTypeId = :aTypeId" \
--expression-attribute-values '{":aTypeId":{"S":"test"}}'
As a precaution, a single Scan
request will only return up to 1MB size of data. If there is anymore data to scan, the response from the request will append LastEvaluatedKey
and that tells you to run Scan
request again to find the item, but this time with --starting-token
and the value is exactly LastEvaluatedKey
If this type of operation is a routine in your case, it will be better to use Global Secondary Index
and use aTypeId
as Primary Key
Upvotes: 2
Reputation: 16157
You field is not in a key or GSI (Global secondary index), then I think you have to use scan
method to get object by aTypeId
,
The query will like:
aws dynamodb scan \
--table-name test \
--filter-expression "aTypeId = :typeValue" \
--projection-expression "aTypeId, aId" \
--expression-attribute-values '{":typeValue":{"S":"test"}}'
If you get back result with LastEvaluatedKey
value, this mean you need take one or more query to get all data:
aws dynamodb scan \
--table-name test \
--filter-expression "aTypeId = :typeValue" \
--projection-expression "aTypeId, aId" \
--starting-token VALUE_NEXT_TOKEN_OF_LAST_QUERY \
--expression-attribute-values '{":typeValue":{"S":"test"}}'
But, I recommended that create a GSI with aTypeId
is hash key will be better.
Upvotes: 5