简体   繁体   English

从 AWS DynamoDB 数据库中检索多个主键值的行

[英]Retrieve rows for multiple primary key values from AWS DynamoDB database

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?从控制台中查看 DynamoDb 页面似乎无法做到这一点,但有没有好的解决方法? Do I just have multiple OR in my KeyConditionExpression and a very complex ExpressionAttributeValues ?我的KeyConditionExpression是否只有多个OR和一个非常复杂的ExpressionAttributeValues

I'm referencing this page: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html我正在引用此页面: 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 https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/dynamodb-example-query-scan.html

您正在查找此处记录的batchGetItem函数。

You can also use DocumentClient and batchGet.您还可以使用 DocumentClient 和 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);
            }
        });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM