【问题标题】:Query dynamodb table by primary key按主键查询dynamodb表
【发布时间】:2019-06-12 04:27:39
【问题描述】:

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

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 似乎没有这样的attribute

这是我的表格在 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-services amazon-dynamodb dynamodb-queries amazon-dynamodb-index


    【解决方案1】:

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

    KeyConditionExpression: 'HashKey = :hkey',
    

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

    KeyConditionExpression: 'id = :hkey',
    

    【讨论】:

    • 你知道,现在看起来很明显......?‍♂️,出于某种原因,我认为这是指钥匙的类型。好的,非常感谢!
    • 我是否会错误地建议您应该真正使用get() 而不是query()?还是有关系?
    • @Michael-sqlbot 回过头来看,我相信你是对的。 Get() 应该做的工作,它更简单一点
    猜你喜欢
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    相关资源
    最近更新 更多