在 Golang 中使用 DynamoDB 查询二级索引
时间:2024-03-04
浏览:492
作者:佚名
来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《在 Golang 中使用 DynamoDB 查询二级索引》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!
问题内容
我的主键是一个名为“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),
},
},
},
},
}
但是;我收到错误“validationexception:查询条件缺少关键架构元素:id”
dynamodb 是否只允许查询主键? 我的印象是,您使用“getitem”作为主键,因为如果您使用主键,则只能返回一条记录。要通过二级索引搜索,请使用“查询”,要通过非索引键搜索,请使用“扫描”。
请让我知道我在这里做错了什么。
解决方案
除了tablename之外,您还需要在创建queryinput来查询索引时指定indexname属性。
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"`
var queryInput, err2 = svc.Query(&dynamodb.QueryInput{
TableName: aws.String(tableName),
IndexName: aws.String(index_name),
KeyConditions: map[string]*dynamodb.Condition{
"Phone": {
ComparisonOperator: aws.String("EQ"),
AttributeValueList: []*dynamodb.AttributeValue{
{
S: aws.String(phone_no),
},
},
},
},
})本篇关于《在 Golang 中使用 DynamoDB 查询二级索引》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
12.01
热门专题
