英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论