我的用例
我有一个表名Test
,分区键为userId
,排序键为campaignId
。我的桌子看起来像这样
"userId (S)","noteId (S)","BName (S)","BValue (S)"
"123_x","123","hi","no"
"123_x","213","how","yes"
"123_x","321","are","yes"
"456_y","456","you","yes"
"456_y","546","i","yes"
"456_y","654","have","yes
现在,我正在尝试检索包含分区键x
的项目。为此,我使用了下面的node.js代码
var AWS = require("aws-sdk");
AWS.config.update({
region: "ap-south-1",
endpoint: "http://dynamodb.ap-south-1.amazonaws.com"
});
var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName : "Test",
ProjectionExpression:"userId,BName,BValue",
KeyConditionExpression: "contains(#userId,:userid)",
ExpressionAttributeNames:{
"#userId": "userId"
},
ExpressionAttributeValues: {
":userid": "x"
}
};
docClient.query(params, function(err, data) {
if (err) {
console.log("Unable to query. Error:", JSON.stringify(err, null, 2));
} else {
console.log("Query succeeded.");
data.Items.forEach(function(item) {
console.log(JSON.stringify(item));
});
}
});
我遇到这样的错误:
Unable to query. Error: {
"message": "Invalid operator used in KeyConditionExpression: contains",
"code": "ValidationException",
"time": "2018-08-23T17:59:01.094Z",
"requestId": "O6PL6NSE1BR7QLGA4U1SOIKGJ7VV4KQNSO5AEMVJF66Q9ASUAAJG",
"statusCode": 400,
"retryable": false,
"retryDelay": 34.77326742946143
}
似乎我不能在关键条件表达式中使用包含。解决方法是什么?
感谢您的帮助
谢谢
答案 0 :(得分:1)
没有一个。执行DynamoDB查询时,您必须始终指定确切的分区键。在不重新设计表的情况下,您唯一的选择是执行扫描并在userId上使用过滤器表达式。
为了避免进行表扫描,请在表上添加一个名为“ userId_suffix”的字段,并使用示例中的“ x”和“ y”类型值填充该字段。然后,您将在“ userId_suffix”上创建一个全局二级索引,并在其上查询“ x”。