DynamoDB – Get Item by Global Secondary Index

I have searched a lot in the documentation and found that if you need to get a single item even then you cant use the get or getItem method when using a global secondary index. One can use the query method. a sample of query method with global secondary index is

let params = {
  TableName: "Users",
  IndexName: "your-index",
  ExpressionAttributeValues: {
    ":v1": "myid"
  }, 
  KeyConditionExpression: "my_partition_key_in_gsi = :v1", 
};
 dynamodb.query(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
 });

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top