0

我正在使用 Lambda 函数,该函数从 dynamoDB 表中的用户 ID 获取用户电子邮件。我使用 dynamoDB 扫描命令扫描 dynamoDB 表中的所有项目。我使用新的 v3 AWS JS 开发工具包。

问题:为什么ExpressionAttributeNames在我的情况下不能正常工作?

这有效:

const params = {    
  FilterExpression: "user_info.user_id = :userid",  
  ExpressionAttributeValues: {
   ":userid": { S: user_id }
  },
  ProjectionExpression: "user_email",
  TableName: aws_table,
}

但这不起作用,为什么?

const params = {
  FilterExpression: "#xyz = :userid",  
  ExpressionAttributeNames: {
    "#xyz": "user_info.user_id"  // <- filter does not work like this (returns 0 findings)
  },
  ExpressionAttributeValues: {
    ":userid": { S: user_id }
  },
  ProjectionExpression: "user_email",
  TableName: aws_table,
};

我的 Lambda 扫描操作代码本身如下所示:

const  { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
const  { ScanCommand } = require("@aws-sdk/client-dynamodb");
const ddbClient = new DynamoDBClient({ region: aws_region });
...  

const run = async () => {
  try {
    const data = await ddbClient.send(new ScanCommand(params));
    data.Items.forEach(function (element, index, array) {
      console.log(element);
    });
    return data;
  } catch (err) {
    console.log("Error", err);
  }
}

await run();

npm i @aws-sdk/client-dynamodb

文档:

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-dynamodb/interfaces/scancommandinput.html#expressionattributenames

4

1 回答 1

1

这是预期的行为

DynamoDB 将表达式属性名称中的点解释为属性名称中的字符

// use multiple expression attribute names if you need a dynamic path:
FilterExpression: "#user_info.#user_id = :userid"
于 2021-12-14T12:56:07.407 回答