我有一个用例,将 begins_with 应用于AWS Dynamodb表的主排序键,
我可以使用来自AWS控制台的begin_with关键条件查询表,
我希望使用AWS Javascript SDK达到相同的目的。
我的表格中有以下字段-
1. user_id(主分区键)
2. user_relation(主排序键)
我尝试了以下代码-
let AWS = require('aws-sdk');
let util = require('util');
AWS.config.update({
region: 'us-east-1'
});
let connection = new AWS.DynamoDB.DocumentClient();
let params = {
TableName: 'user_details',
KeyConditionExpression: 'user_id = :user_id and user_relation begins_with :user_relation',
ExpressionAttributeValues: {
':user_id': "1234",
':user_relation': "followed-by"
}
};
console.log('getQuery Params => ', params);
let dynamoDb = util.promisify(connection.query).bind(connection);
let results = await dynamoDb(params);
console.log('results => ', results);
我收到语法错误,查询AWS DynamoDb表时使用 begins_with 的正确方法是什么?
错误-
(node:40474) UnhandledPromiseRejectionWarning: ValidationException: Invalid KeyConditionExpression: Syntax error; token: "begins_with", near: "user_relation BE
GINS_WITH :user_relation"
参考-
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#query-property
DynamoDB,how to query with BEGINS_WITH
Can't Query DynamoDB with Begins_with for list of Items
答案 0 :(得分:1)
尝试:
const params = {
TableName: 'user_details',
KeyConditionExpression: '#user_id = :user_id and begins_with(#user_relation, :user_relation)',
ExpressionAttributeNames:{
"#user_id": "user_id",
"#user_relation": 'user_relation'
},
ExpressionAttributeValues: {
":user_id": "1234",
":user_relation": "followed-by"
}
}