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

Scanning DynamoDB table not showing results from the column specified in ProjectionExpression

我正在尝试扫描 DynamoDB 故事,以便只获取过滤后的项目。 myTable 有 3 columns: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 Scanning table. 2018-05-22T08:04:21.672Z baac6d92-5d96-11e8-ae78-bd04c275acf5 { Items: [ { userId: 'amzn1.ask.account.XYZ' } ], Count: 1, ScannedCount: 2 }

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

我做错了什么?

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

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