javascript - 如何使用排序键中的任何值查询 DynamoDB?
问题描述
我有一个包含 userId、pictureURL 和其他一些字段的简单表。我想返回具有某个 userId 的所有字段,但是当我这样做时
dynamodb.get({
TableName: tableName,
Key: {
'userid': '39e1f6cb-22af-4f8c-adf5-xxxxxxxxxx'
}
}, ...
我明白The provided key element does not match the schema
了,因为它似乎也需要排序键。当我做
dynamodb.get({
TableName: tableName,
Key: {
'userid': '39e1f6cb-22af-4f8c-adf5-xxxxxxxxxx',
'pictureurl': '' // Or null
}
}, ...
我收到一个错误One or more parameter values were invalid: An AttributeValue may not contain an empty string
那么如何查询排序键中的任何值呢?
解决方案
使用 DynamoDB DocumentClient:
因此,使用查询并使用KeyConditionExpression
参数为分区键提供特定值。查询操作将返回具有该分区键值的表(或索引)中的所有项目。
这是一个例子:
const AWS = require("aws-sdk");
AWS.config.update({region: 'us-east-1'});
const params = {
TableName: tableName,
KeyConditionExpression: '#userid = :userid',
ExpressionAttributeNames: {
'#userid': 'userid',
},
ExpressionAttributeValues: {
':userid': '39e1f6cb-22af-4f8c-adf5-xxxxxxxxxx',
},
};
const dc = new AWS.DynamoDB.DocumentClient();
dc.query(params, (err, data) => {
if (err) {
console.log('Error', err);
} else {
for (const item of data.Items) {
console.log('item:', item);
};
}
});
推荐阅读
- c - 置换基本 BNF 样式字符串
- php - htmlspecialchars() 期望参数 1 在 Laravel 5.8 中是字符串
- jquery - 在调整窗口浏览器大小时启用/禁用链接中的标题
- apache-kafka - Kafka Connect 接收器任务忽略容差限制
- jquery - Worpdress 上的 Google Analytics 事件跟踪
- angular - 在 angular7 中实现引导模式对话框
- angular - 从本地 Angular 库自动导入包括“public-api”并中断应用程序
- javascript - 如何将 react-boostrap 与 next.js 和 React.js 一起使用
- amazon-web-services - 如何设置我的 API 以要求使用 amazon API Gateway 的 API 密钥?
- hibernate - JSON没有被Jackson隐式转换为RequestBody对象依赖于Spring Mvc中的发布请求?