node.js - “提供的关键元素与二级索引的架构不匹配”

标签 node.js amazon-web-services amazon-dynamodb

我有一个描述的 DynamoDB 表(应通过唯一的 user_id 或用户名 + system_id 的组合来选择用户):

"TableName": "auth-users",
"KeySchema": [ {
        "KeyType": "HASH", 
        "AttributeName": "user_id"
    }
], 
"AttributeDefinitions": [ {
    {
        "AttributeName": "user_id", 
        "AttributeType": "S"
    },
        "AttributeName": "system_id", 
        "AttributeType": "S"
    },  {
        "AttributeName": "username", 
        "AttributeType": "S"
    }
], 
"GlobalSecondaryIndexes": [
    {
        "IndexName": "username-system_id-index", 
        "Projection": {
            "ProjectionType": "ALL"
        }, 
        "IndexStatus": "ACTIVE", 
        "KeySchema": [ {
               "KeyType": "HASH", 
               "AttributeName": "username"
            }, {
               "KeyType": "RANGE", 
               "AttributeName": "system_id"
            }
        ]
    }
],
... 

当我使用主键运行以下代码时,我得到了预期的记录列表:

var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({region: 'ap-southeast-2');
var userTable = 'auth-users';
var key = { user_id: '123412341234' };

dynamodb.getItem({
   TableName: tableName,
   Key: key
}, function(err,data) { 
   console.error('err:',err); 
   console.info('data:', data); 
});

...但是如果我尝试使用全局二级索引,请将键替换为:

var key = {
    username:  {S: 'testuser'}, 
    system_id: {S: 'test-system'}
};

然后我得到一个ValidationException:提供的关键元素与架构不匹配

最佳答案

...没关系 - RTFM:

The GetItem operation returns a set of attributes for the item with the given primary key

要使用二级索引,您需要执行 query并提供索引名称。

关于node.js - “提供的关键元素与二级索引的架构不匹配”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40355490/

相关文章:

node.js - Node-Redshift 是否支持复制命令(查询)将数据从 S3 加载到 Redshift?

javascript - Node.JS async.parallel 不会等到所有任务都完成

twitter - Amazon Web Service 微型实例 - 服务器崩溃

amazon-web-services - 使用不区分大小写的条件查询 DynamoDB

node.js - 无服务器框架的最佳实践

javascript - 如何在 Node.JS 中将 Json Web Token 拆分为 Header-Payload-Signature

node.js - 在 Azure 上运行 NodeJS 命令

java - AWS 中的可用区选择

amazon-web-services - 是否可以在 Dynamo 的 PartiQL 中使用 JOIN?

python - DynamoDB 哈希键 int 与字符串