dynamodb get all items by an array of ids

thatsIT picture thatsIT · Nov 3, 2018 · Viewed 7.9k times · Source

I have a table with an attribute with name id and of type HASH. I want to get all items from a array of id's.

{
    TableName: `MyTable`,
    FilterExpression: 'id IN (:id)',
    ExpressionAttributeValues: { ':id': ids },
};

What should I do to get all items by my ids?

Answer

IftekharDani picture IftekharDani · Nov 4, 2018

You can also use DocumentClient and batchGet.

const AWS = require('aws-sdk');
const documentClient = new AWS.DynamoDB.DocumentClient();

let queryParams = {RequestItems: {}};
queryParams.RequestItems['tableName'] = {
  Keys: [{'id': 'Value1'}, {'id': 'value2'}],
  ProjectionExpression: 'id' //define other fileds that you have Ex: 'id,name'
};

documentClient.batchGet(queryParams, function (err, data) {
  if (err) {
    console.log('failure:getItemByBatch data from Dynamo error', err);
  } else {
    console.log('success:getItemByBatch data from Dynamo data');
    console.log(data)
  }
});