这是我的dynamodb表,它包含分区和排序键,我可以仅根据国家/地区获取数据吗?
Type: 'AWS::DynamoDB::Table'
Properties:
AttributeDefinitions:
- AttributeName: 'provinceId'
AttributeType: 'S'
- AttributeName: 'countryId'
AttributeType: 'S'
BillingMode: PAY_PER_REQUEST
TableName: 'province-country-list'
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: true
Tags:
- Key: 'STAGE'
Value: '${self:provider.stage}'
- Key: 'CostCenter'
Value: !Sub '${self:provider.stage}-${AWS::AccountId}'
KeySchema:
- AttributeName: 'provinceId'
KeyType: 'HASH'
- AttributeName: 'countryId'
KeyType: 'RANGE'
# ProvisionedThroughput:
# ReadCapacityUnits: 1
# WriteCapacityUnits: 1
这就是我尝试过的
const params = {
TableName: 'province-country-list',
KeyConditionExpression: 'countryId = :countryId',
ExpressionAttributeValues: {
':countryId': countryId,
},
};
const provincesList = await dynamoDb.query(params).promise();
得到 查询条件缺少关键架构元素:provinceId
不,您不能仅在
countryId
上搜索,因为它是您的排序键,但您可以使用 provinceId
进行搜索,因为这是您的分区键。
您必须提供分区键属性的名称以及该属性的单个值。查询返回具有该分区键值的所有项目。或者,您可以提供排序键属性并使用比较运算符来优化搜索结果。
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html
为了能够使用
countryId
进行搜索,请考虑创建 全球二级索引