【问题标题】:Scanning DynamoDB table not showing results from the column specified in ProjectionExpression扫描 DynamoDB 表未显示 ProjectionExpression 中指定的列的结果
【发布时间】:2018-10-31 22:32:01
【问题描述】:

我正在尝试扫描 DynamoDB 故事,以便仅获取过滤后的项目。 myTable有3列:L timestamp (String, PK), colors (String), userId (String)

var docClient = new AWS.DynamoDB.DocumentClient();

var params = {
    "TableName": "myTable",
    "ProjectionExpression": "colors, userId",
    "ExpressionAttributeValues": {":val": userId},
    "FilterExpression": "userId = :val"

};

console.log("Scanning table.");
docClient.scan((params), function(err,data){

    if (err) console.log(err, err.stack); 
    else console.log(data); //success response
});

2018-05-22T08:04:21.395Z baac6d92-5d96-11e8-ae78-bd04c275acf5 扫描表。 2018-05-22T08:04:21.672Z baac6d92-5d96-11e8-ae78-bd04c275acf5 {项目:[{userId:'amzn1.ask.account.XYZ'}],计数:1,ScannedCount:2}

因此,我只能从 userId 列中获取值。 'colors' 列被完全忽略。

我做错了什么?

【问题讨论】:

    标签: node.js aws-lambda amazon-dynamodb alexa-skills-kit alexa-skill


    【解决方案1】:

    ExpressionAttributeNames 中设置ProjectionExpression 属性。看看下面的sn-p

    var docClient = new AWS.DynamoDB.DocumentClient();
    
     var params = {
      ExpressionAttributeNames: {
       "#colors": "colors", 
       "#userId": "userId"
      }, 
      ExpressionAttributeValues: {
        ":val": userId
      }, 
      FilterExpression: "userId = :val", 
      ProjectionExpression: "#colors, #userId", 
      TableName: "myTable"
     };
    
    console.log("Scanning table."); 
    
    docClient.scan(params, function(err, data) {
        if (err) console.log(err, err.stack); 
        else console.log(data); //success response
    });
    

    注意#: 必须分别出现在ExpressionAttributeNamesExpressionAttributeValues 上。

    【讨论】:

    • 感谢您的建议,但是在实施您的更改后,我收到另一个错误 ValidationException: ExpressionAttributeNames contains invalid key: Syntax error;键:“userId”
    • 谢谢,如果可能的话,用您提供的修复程序编辑我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多