0

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"
        }
      }
Maurice
  • 11,482
  • 2
  • 25
  • 45
user686483
  • 1,584
  • 6
  • 18
  • 29
  • What is your question? What are the keys in your table? – Maurice Nov 11 '21 at 11:13
  • Apologies. I added the keys into the question. I'm wanting to basically return a row by querying on two attributes. – user686483 Nov 11 '21 at 11:19
  • If you can rely on a sort-order you can limit like this: https://stackoverflow.com/questions/9297326/is-it-possible-to-order-results-with-query-or-scan-in-dynamodb/36883510#36883510 – kometen Nov 11 '21 at 11:21

1 Answers1

3

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.

Maurice
  • 11,482
  • 2
  • 25
  • 45
  • If the OP is using the primary key in `Query()`, he's only going to get a single row back anyway. You have to use something other than `=` on the sort key to possibly get back multiple records. – Charles Nov 11 '21 at 14:37
  • 1
    @Charles true, I think parsing the result is easier with `GetItem` though as there is one nested structure less and if I recall correctly the failure mode is different - i.e. `GetItem` on a non-existent item raises an Exception in some SDKs while an empty result list doesn't in `Query` – Maurice Nov 11 '21 at 16:37