如何从AWS Lambda中的DynamoDB列表中删除元素?

时间:2018-04-29 21:32:57

标签: amazon-web-services aws-lambda amazon-dynamodb aws-sdk

here is the image of data in dynamodb

我想删除favRest中的元素,我只需要给出值,这样就可以了,这就是我的lambda函数

var AWS = require('aws-sdk');

const docClient = new AWS.DynamoDB.DocumentClient({ region: 'us-east-1' });

exports.handler = (event, context, callback) => {
    const params = {
        TableName : 'User',
        Key:{
            "id": event.user_id,
        },
        UpdateExpression: "DELETE favRest :p",
        ExpressionAttributeValues: {
            ':p': event.place_id
        },
        ReturnValues: "ALL_NEW" 
    }

    // TODO: Implementation...

    docClient.update(params, (err, data) => {
        if (err) {
            console.log("Unable to update item. Error: " + err.message);
            callback(err);
        } else {
            console.log("UpdateItem succeeded.");
            callback(null, data);
        }
    });
};

然而,它给了我以下错误:

"{\n  \"message\": \"Invalid UpdateExpression: Incorrect operand type for operator or function; operator: DELETE, operand type: STRING\",\n  \"code\": \"ValidationException\",\n  \"time\": \"2018-04-29T18:49:58.628Z\",\n  \"requestId\": \"7TDRI4TOF9S71OUJDEKMIOA40RVV4KQNSO5AEMVJF66Q9ASUAAJG\",\n  \"statusCode\": 400,\n  \"retryable\": false,\n  \"retryDelay\": 35.34160854636804\n}"

我应该怎么做

1 个答案:

答案 0 :(得分:1)

DELETE操作仅支持Set数据类型。您的favRest属性类型为List

如果您希望将favRest属性类型保留为List,则可以使用REMOVE从列表中删除单个元素(了解元素的索引):

UpdateExpression: "REMOVE favRest[:index]"

或者您可以使用SET将新列表替换为新值:

UpdateExpression: "SET favRest = :newList"

否则,您可以将favRest属性类型更改为Set

请参阅Update Expressions