AWS SDK JavaScript v3 / 如何在 dynamoDB 扫描命令中使用 ExpressionAttributeNames?
AWS SDK JavaScript v3 / How to use ExpressionAttributeNames within dynamoDB Scan Command?
我正在使用 Lambda 函数,该函数从 dynamoDB table 中的用户 ID 获取用户电子邮件。我使用 dynamoDB 扫描命令扫描 dynamoDB table 中的所有项目。我使用新的 v3 AWS JS SDK。
问题:为什么 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
文档:
DynamoDB interprets a dot in an expression attribute name as a character within an attribute's name
// use multiple expression attribute names if you need a dynamic path:
FilterExpression: "#user_info.#user_id = :userid"
我正在使用 Lambda 函数,该函数从 dynamoDB table 中的用户 ID 获取用户电子邮件。我使用 dynamoDB 扫描命令扫描 dynamoDB table 中的所有项目。我使用新的 v3 AWS JS SDK。
问题:为什么 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
文档:
DynamoDB interprets a dot in an expression attribute name as a character within an attribute's name
// use multiple expression attribute names if you need a dynamic path:
FilterExpression: "#user_info.#user_id = :userid"