vogels icon indicating copy to clipboard operation
vogels copied to clipboard

Querying by HASH key only from GSI

Open nygardk opened this issue 9 years ago • 3 comments

How is it possible to fetch only by hash-key from a GSI?

E.g. I have this table

  {
    TableName: 'contact',
    KeySchema: [
      { AttributeName: 'id', KeyType: 'HASH'},
    ],
    AttributeDefinitions: [
      { AttributeName: 'id', AttributeType: 'S' },
      { AttributeName: 'account', AttributeType: 'S' },
    ],
    ProvisionedThroughput: {
      ReadCapacityUnits: 5,
      WriteCapacityUnits: 3,
    },
    GlobalSecondaryIndexes: [
      {
        IndexName: 'accountIndex',
        KeySchema: [
          { AttributeName: 'account', KeyType: 'HASH' },
        ],
        Projection: {
          ProjectionType: 'ALL',
        },
        ProvisionedThroughput: {
          ReadCapacityUnits: 3,
          WriteCapacityUnits: 3,
        },
      },
    ],
  }

And I would like to execute this query with vogels:

docClient.query({
    TableName: 'contact',
    IndexName: 'accountIndex',
    KeyConditionExpression: 'account = :account',
    ExpressionAttributeValues: { ':account': 'myaccount' },
}, callback);

nygardk avatar May 26 '16 10:05 nygardk

Perhaps something like this: ?

Contact
  .query('myaccount')
  .usingIndex('accountIndex')
  .exec(callback);

You know that there is a documentation about this :-) There is also expression API that you may use if you don't like this method.

set-killer avatar May 26 '16 20:05 set-killer

@set-killer Thanks for the suggestion. I tried it already, and it responds with error MissingRequiredParameter: Missing required key 'Key' in params. All the examples in the documentation use both hash and range key for the queries, which made me question if the above is even possible.

The problem remains.

nygardk avatar May 27 '16 07:05 nygardk

@set-killer gave you the answer about how using an Index. Your error means you didn't pass the id in 1st parameter

Can you paste you query corrected please ?

sylwit avatar May 27 '16 19:05 sylwit