首页 > 解决方案 > 按主键查询dynamodb表

问题描述

我在创建简单查询请求时遇到问题。这里,他们展示了如何使用全局二级索引进行查询的示例。现在在这种情况下,我只有主键,我想从我的表中查询。这是目前我得到的错误:

Query condition missed key schema element: id

这是我目前正在尝试的:

var params = {
  TableName : "XactRemodel-7743-DynamoDBImagesTable-8183NQ0UG0Y5",
  KeyConditionExpression: 'HashKey = :hkey',
  ExpressionAttributeValues: {
    ':hkey': event.projectId
  }
};
documentClient.query(params, function(err, data) {
   if (err) {
     console.log(err)
   } else {
     console.log(data);
   }
});

我知道在示例中他们使用了与二级索引名称相对应的“indexName”。他们的 Key Schema 似乎没有这样的属性

这是我的表在 YAML 文件中定义的样子:

DynamoDBImagesTable:
Type: AWS::DynamoDB::Table
Properties:
  BillingMode: PAY_PER_REQUEST
  SSESpecification:
    SSEEnabled: true
  PointInTimeRecoverySpecification:
    PointInTimeRecoveryEnabled: true
  AttributeDefinitions:
    - AttributeName: id
      AttributeType: S
    - AttributeName: companyId
      AttributeType: S
    - AttributeName: lastModified
      AttributeType: S
  KeySchema:
    - AttributeName: id
      KeyType: HASH
  GlobalSecondaryIndexes:
    - IndexName: companyId-index
      KeySchema:
        - AttributeName: companyId
          KeyType: HASH
        - AttributeName: lastModified
          KeyType: RANGE
      Projection:
        ProjectionType: ALL

我错过了什么?

标签: amazon-web-servicesamazon-dynamodbdynamodb-queriesamazon-dynamodb-index

解决方案


这是尝试通过名为的主键查询HashKey

KeyConditionExpression: 'HashKey = :hkey',

但是,您的主键被命名id,这就是错误消息所指出的。所以将该行更改为:

KeyConditionExpression: 'id = :hkey',

推荐阅读