DynamoDB 查询输入:如何检索键值不为空的所有项目



>我有以下模式:

resources:
Resources:
ChatDynamoDbTable:
Type: "AWS::DynamoDB::Table"
DeletionPolicy: Retain
Properties:
TableName: "chats-admin-${opt:stage, self:provider.stage}"
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
AttributeDefinitions:
- AttributeName: id
AttributeType: S         
- AttributeName: user_id
AttributeType: S         
- AttributeName: admin_type
AttributeType: S         
KeySchema:
- AttributeName: id
KeyType: HASH
GlobalSecondaryIndexes:
-
IndexName: user_id-admin_type-index
KeySchema:
-
AttributeName: user_id
KeyType: HASH
-
AttributeName: admin_type
KeyType: RANGE
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1

我需要获取admin_type与我提供的密钥匹配的所有项目。

我尝试了以下内容,但它失败了,因为我没有指定user_id的值:

"error_message":"验证异常:查询条件缺少键 架构元素:user_id

input := &dynamodb.QueryInput{
TableName:              aws.String(table),
IndexName:              aws.String("user_id-admin_type-index"),
KeyConditionExpression: aws.String("#admin_type = :admin_type"),
ExpressionAttributeNames: map[string]*string{
"#admin_type": aws.String("admin_type"),
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":admin_type": {
S: aws.String(a.AdminType),
},
},
}

如何修改上述内容以获取admin_type与我的密钥匹配的所有项目?(基本上所有项目都匹配admin_type而不用担心user_id的价值。

DynamoDB API 提供了两种获取数据的基本方法;queryscan.

query操作要求您指定主键。 当您具有复合主键(分区键 + 排序键)时,您可以:

  • 仅指定分区键(你将取回分区中的所有内容)
  • 同时指定分区键和排序键(你将返回单个项目)

query操作没有仅指定排序键的选项。

按排序键提取项目,您需要使用scan操作。 或者,您可以创建一个二级索引,并将主键设置为排序键(在您的情况下为admin_type)。 这将允许您按admin_type获取项目,而不管user_id如何。

最新更新