Golang获取DynamoDB中每个属性的电话项。

huangapple go评论118阅读模式
英文:

Golang get item per attibute phone dynamodb

问题

我正在查询 DynamoDB 中的一个值,但是我收到了错误消息 "提供的键元素与模式不匹配"。

  1. params := &dynamodb.GetItemInput{
  2. Key: map[string]*dynamodb.AttributeValue{
  3. "phone": { // 必填项
  4. S: aws.String(phone),
  5. },
  6. },
  7. TableName: aws.String(TableName),
  8. }

请注意,你需要将 phone 替换为你要查询的电话号码,并将 TableName 替换为你的表名。

英文:

I am querying a value in dynamodb but I get the error "The supplied key element does not match the schema".

  1. {
  2. "id": {
  3. "S": "c8f8fd5d-a483-4a4e-8adf-75c4134678cc"
  4. },
  5. "phone": {
  6. "S": "+57308394111288"
  7. },
  8. }

Golang

  1. params := &dynamodb.GetItemInput{
  2. Key: map[string]*dynamodb.AttributeValue{
  3. "phone": { // Required
  4. S: aws.String(phone),
  5. },
  6. },
  7. TableName: aws.String(TableName),
  8. }

答案1

得分: 1

感谢您的消息。解决方法如下:

  1. filt := expression.Name("phone").Equal(expression.Value(phone))
  2. expr, err := expression.NewBuilder().WithFilter(filt).Build()
  3. params := &dynamodb.ScanInput{
  4. ExpressionAttributeNames: expr.Names(),
  5. ExpressionAttributeValues: expr.Values(),
  6. FilterExpression: expr.Filter(),
  7. ProjectionExpression: expr.Projection(),
  8. TableName: aws.String(tableName),
  9. }
英文:

thanks per messages.solved this way

  1. filt := expression.Name("phone").Equal(expression.Value(phone))
  2. expr, err := expression.NewBuilder().WithFilter(filt).Build()
  3. params := &dynamodb.ScanInput{
  4. ExpressionAttributeNames: expr.Names(),
  5. ExpressionAttributeValues: expr.Values(),
  6. FilterExpression: expr.Filter(),
  7. ProjectionExpression: expr.Projection(),
  8. TableName: aws.String(tableName),
  9. }

huangapple
  • 本文由 发表于 2022年8月3日 23:36:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/73224032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定