扫描DynamoDB表未显示ProjectionExpression中指定的列的结果

问题描述 投票:0回答:1

我正在尝试扫描DynamoDB故事,以便我只获取过滤的项目。 myTable有3列:L时间戳(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 {Items:[{userId:'amzn1.ask.account.XYZ'}],Count:1,ScannedCount:2}

因此,我只从userId列获取值。 “颜色”列完全被忽略。

我究竟做错了什么?

node.js aws-lambda amazon-dynamodb alexa-skills-kit alexa-skill
1个回答
3
投票

ProjectionExpression中设置ExpressionAttributeNames属性。查看以下代码段

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上。

© www.soinside.com 2019 - 2024. All rights reserved.