我正在尝试在DynamoDB中向List(评论)添加新Map,但我似乎无法正确使用它。下面是表格的简单结构:
{
"Comments": [
{
"Body": "This is the first comment",
"Username": "admin"
},
{
"Body": "This is the second comment",
"Username": "Jenny"
},
{
"Body": "This is the third comment",
"Username": "Bob"
}
],
"PostId": "RY28q1AxhR9Qi2VjrDdUdo5bPXBybUg2"
}
PHP代码:
$response = $ddb->updateItem (
[
'TableName' => 'comments',
'Key' => [
'PostId' => ['S' => $request->input('postid')]
],
"ExpressionAttributeNames" => ["#theList" => "Comments"],
"ExpressionAttributeValues" => [
':addVal' => [
'M' => [
'Username' => ['S' => 'MrSmith'],
'Body' => ['S' => 'Hello this is a comment'],
]
]
],
'UpdateExpression' => 'SET #theList = list_append(:addVal, #theList)',
]);
我收到此错误:
{
"result": 1,
"error": {
"message": "Error executing \"UpdateItem\" on \"https://dynamodb.us-east-1.amazonaws.com\"; AWS HTTP error: Client error: 400 ValidationException (client): Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: M - {\"__type\":\"com.amazon.coral.validate#ValidationException\",\"message\":\"Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: M\"}",
"status_code": "ValidationException"
}
任何帮助将不胜感激。感谢
编辑:仍然坚持这个问题,请帮忙吗?感谢
答案 0 :(得分:4)
我遇到了同样的问题,思想在这里和那里得到了解释,这是最终解决的问题
$res =$this->dynamodb->updateItem([
'TableName' => 'your_table',
'Key' => [
'key1' => ['N' => $detail1,
'key2' => ['S' => $detail2,
"ExpressionAttributeNames" => ["#theList" => "my_list_iten_name"],
"ExpressionAttributeValues" => [
':empty_list' => ['L'=>[]],
':addVal' => [
'L' => [
[
'M' => [
'val1' => ['S' => 'one'],
'val2' => ['S' => 'two!!!'],
]
],
[
'M' => [
'val1' => ['S' => 'one1'],
'val2' => ['S' => 'two2!!!'],
]
]
]
]
],
'UpdateExpression' => 'SET #theList = list_append(if_not_exists(#theList, :empty_list), :addVal)',
]);
答案 1 :(得分:3)
作为一名javascript开发人员,当我将Object(Map)包装在一个数组(List)中时,我解决了我的问题,它看起来像这样。
var params = {
TableName: 'tableName',
Key: {
Id: "myId",
},
UpdateExpression: 'SET theList = list_append(theList, :value)',
ExpressionAttributeValues: {
':value': [{test: 'data'}]
},
ReturnValues: 'UPDATED_NEW',
};
docClient.update(params, function(err, data) {
if (err) ppJson(err); // an error occurred
else ppJson(data); // successful response
});
据我所知,list_append
用于将列表附加到另一个列表中,我担心这是唯一的方法。
根据您所写的内容,您可以尝试:
"ExpressionAttributeValues" => [
':addVal' => [
'L' => [[
'M' => [
'Username' => ['S' => 'MrSmith'],
'Body' => ['S' => 'Hello this is a comment'],
]
]]
]
],
答案 2 :(得分:0)
我认为您可以使用ADD
结果调用SET
运算符而不是list_append
。