使用Go更新DynamoDB中的多个值

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

Updating multiple values in DynamoDB with Go

问题

我正在尝试更新DynamoDB表中的value1和value2项属性,但是UpdateExpression只设置了一个参数(value1)。
尝试同时包含value1和value2(aws.String("set value1= :r, set value2= :c"))会导致以下错误:

调用UpdateItem时出错:%s
ValidationException: Invalid UpdateExpression: Syntax error; token: "set", near: ", set 
HashValue" status code: 400, request id:******

如何在数据库项中更新多个字段?

--

func UpdateObject(obj MyObject) (string, error) {

    sess := session.Must(session.NewSessionWithOptions(session.Options{
        SharedConfigState: session.SharedConfigEnable,
    }))

    // 创建DynamoDB客户端
    svc := dynamodb.New(sess)

    // 在表Movies中更新项
    tableName := "MyTable"

    input := &dynamodb.UpdateItemInput{
        ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
            ":r": {
                N: aws.String(strconv.Itoa(obj.value1)),
            },
            ":c": {
                S: aws.String(obj.value2),
            },
        },
        TableName: aws.String(tableName),
        Key: map[string]*dynamodb.AttributeValue{
            "ThreatID": {
                N: aws.String(strconv.Itoa(obj.value3)),
            },
            "ThreatName": {
                S: aws.String(obj.value4),
            },
        },
        ReturnValues:     aws.String("UPDATED_NEW"),
        UpdateExpression: aws.String("set value1= :r"),
    }

    //........

}

--

type MyObject struct {
    value3 int
    value4 string
    value2 string
    value1 int
}
英文:

I am trying to update both value1 and value2 item attributes in DynamoDb Table, but the UpdateExpression sets only one argument (value1).
Trying to include both value1 and value2 (aws.String("set value1= :r, set value2= :c")) results in:

Got error calling UpdateItem: %s
ValidationException: Invalid UpdateExpression: Syntax error; token: "set", near: ", set 
HashValue" status code: 400, request id:******

How can I update more than one field in the Data Base item ?

--

func UpdateObject(obj MyObject) (string, error) {

sess := session.Must(session.NewSessionWithOptions(session.Options{
	SharedConfigState: session.SharedConfigEnable,
}))

// Create DynamoDB client
svc := dynamodb.New(sess)

// Update item in table Movies
tableName := "MyTable"

input := &dynamodb.UpdateItemInput{
	ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
		":r": {
			N: aws.String(strconv.Itoa(obj.value1)),
		},
		":c": {
			S: aws.String(obj.value2),
		},
	},
	TableName: aws.String(tableName),
	Key: map[string]*dynamodb.AttributeValue{
		"ThreatID": {
			N: aws.String(strconv.Itoa(obj.value3)),
		},
		"ThreatName": {
			S: aws.String(obj.value4),
		},
	},
	ReturnValues:     aws.String("UPDATED_NEW"),
	UpdateExpression: aws.String("set value1= :r"),
}

 //........

}

--

 type MyObject struct {
     value3 int
     value4 string
     value2 string
     value1 int
   }

答案1

得分: 3

使用以下语法:

aws.String("set value1= :r, value2= :c")

参考链接:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html

英文:

Use this syntax:

aws.String("set value1= :r, value2= :c")

Reference:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html

huangapple
  • 本文由 发表于 2021年6月6日 23:18:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/67860674.html
匿名

发表评论

匿名网友

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

确定