在 Golang 中查询 DynamoDB 上的二级索引

use*_*263 3 go amazon-dynamodb dynamodb-queries amazon-dynamodb-index

我的主键是一个名为“id”的字段

我在“group_number”字段上的表中添加了二级索引

在此处输入图片说明

我通过二级索引查询,如下所示:

// Query the secondary index
queryInput := &dynamodb.QueryInput{
    TableName: aws.String(c.GetDynamoDBTableName()),
    KeyConditions: map[string]*dynamodb.Condition{
        "group_number": {
            ComparisonOperator: aws.String("EQ"),
            AttributeValueList: []*dynamodb.AttributeValue{
                {
                    S: aws.String(c.GroupNumber),
                },
            },
        },
    },
}
Run Code Online (Sandbox Code Playgroud)

然而; 我收到错误“validationexception:查询条件缺少关键架构元素:id”

DynamoDB 是否只允许查询主键?我的印象是您使用“GetItem”作为主键,因为如果您使用主键,则只有一条记录可以返回。要通过二级索引进行搜索,请使用“查询”,要通过非索引键进行搜索,请使用“扫描”。

请让我知道我在这里做错了什么。

eug*_*ioy 5

除了TableName之外,IndexName在创建QueryInput查询索引时还需要指定属性。

https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/#QueryInput

// The name of an index to query. This index can be any local secondary index
// or global secondary index on the table. Note that if you use the IndexName
// parameter, you must also provide TableName.
IndexName *string `min:"3" type:"string"`
Run Code Online (Sandbox Code Playgroud)