osotorrio
osotorrio

Reputation: 1162

Querying a Global Secondary Index: 'MissingRequiredParameter: Missing required key

I have a DynamoDB table definition like this one:

{
"pk": {
  "S": "00000000-0000-0000-0000-000000000001"
},
"sk": {
  "S": "00000000-0000-0000-0000-000000000001"
},
"year": {
  "N": "2019"
}
"index": {
  "N": "987654321"
}
}

Where "pk" is the Partition Key. And "sk" is the Sort Key. If you are wondering why both, pk and sk, have the same value: Adjacency List Design Pattern.

I have created a Global Secondary Index on the attribute "index". Which its projections are both keys, pk and sk.

enter image description here

Now, following the documentation I am querying my GSI like this:

        const params = {
                TableName: 'my_table_name',
                IndexName: 'my_index_name',
                KeyConditionExpression: "index = :v_index",
                ExpressionAttributeValues: {
                    ":v_index": {"N": 987654321}
                },
                ProjectionExpression: "pk",
                ScanIndexForward: false
            };

I am receiving the following error message: 'MissingRequiredParameter: Missing required key \'Key\' in params\n

Just in case, I am using Node.js 8.10 in Lambda

Upvotes: 0

Views: 1164

Answers (1)

Rahul
Rahul

Reputation: 478

In general, GSIs can have multiple entries for the same GSI primary key + secondary key combination. As a result you cannot use a getItem() call to retrieve objects. Queries against a GSI need to either be scan or a query

This is mentioned in the first few lines of Amazon's documentation about working with GSIs

Upvotes: 0

Related Questions