3

I am trying to say:

select * from myTable where pkName in ('john', 'fred', 'jane')

but there doesn't seem to be a native way to feed a list of items in an array. I have my query working and retrieving values for a single primary key but want to be able to pass in multiple ones. It seems this isn't possible from looking at the DynamoDb page in the console but is there a good workaround? Do I just have multiple OR in my KeyConditionExpression and a very complex ExpressionAttributeValues?

I'm referencing this page: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html

And using code based on the following (which can be found at the address below):

var params = {
  ExpressionAttributeValues: {
    ':s': {N: '2'},
    ':e' : {N: '09'},
    ':topic' : {S: 'PHRASE'}
  },
  KeyConditionExpression: 'Season = :s and Episode > :e',
  ProjectionExpression: 'Title, Subtitle',
  FilterExpression: 'contains (Subtitle, :topic)',
  TableName: 'EPISODES_TABLE' 
};

https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/dynamodb-example-query-scan.html

2 Answers 2

7

You are looking for the batchGetItem function, documented here.

4

You can also use DocumentClient and batchGet.

const AWS = require('aws-sdk');
const dbClient = new AWS.DynamoDB.DocumentClient({ region: 'ap-south-1' });

exports.handler = (event, context, callback) => {
        
        var cartItems=JSON.parse(event.body);
         let scanningtable =  {
             RequestItems: {
               COOLERS : {
                    Keys: [
                        {
                           "ITEM_ID": 379
                         },
                         {
                           "ITEM_ID": 376
                         }
                    ], 
                    ProjectionExpression: "ITEM_ID, #N,CATEGORY, SUB_CATEGORY, BRAND, SELL_RATE",
                    ExpressionAttributeNames: {
                        "#N": "NAME" 
                    },
                }
             }
          };

        dbClient.batchGet(scanningtable, function (err, data) {
            if (err) {
                callback(err, null);
            } else {
                var response = {
                    "statusCode": 200,
                    "headers": {
                        "Access-Control-Allow-Origin": "*"
                    },
                    "body": JSON.stringify(data),
                };
                callback(null, response);
            }
        });
};

Not the answer you're looking for? Browse other questions tagged or ask your own question.