Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 要检索特定分区密钥结构的结果吗?_Node.js_Amazon Dynamodb_Dynamodb Queries - Fatal编程技术网

Node.js 要检索特定分区密钥结构的结果吗?

Node.js 要检索特定分区密钥结构的结果吗?,node.js,amazon-dynamodb,dynamodb-queries,Node.js,Amazon Dynamodb,Dynamodb Queries,我的用例 我有一个表名测试,分区键作为用户ID,排序键作为活动ID。我的桌子看起来像这样 "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 现

我的用例

我有一个表名测试,分区键作为用户ID,排序键作为活动ID。我的桌子看起来像这样

"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
}
似乎我不能在keycondition表达式中使用contains。解决这个问题的方法是什么

谢谢你的帮助


谢谢,没有了。在执行DynamoDB查询时,必须始终指定确切的分区键。在不重新设计表的情况下,您唯一的选择是对userId执行扫描并使用过滤器表达式

为了避免表扫描的开销,您可以在表上添加一个名为userId\u后缀的字段,并用示例中的x和y类型值填充它。然后,您将在userId_后缀上创建一个全局二级索引,并在该后缀上查询x