删除dynamodb中的嵌套属性

时间:2017-07-18 14:47:44

标签: node.js amazon-web-services amazon-dynamodb

我想说我在DynamoDB中有这个项目:

{
  "customerId": "customer_001",
  "customerName": "itsme",
  "address": {
    "city": "Frankfurt",
    "country": "Germany",
    "street1": "c/o Company xxx",
    "street2": "Europe",
    "street3": "PO Box 406",
    "zip": "12345"
  }
}

我需要从项目中删除嵌套属性address.street3。我怎么能做到这一点?

以下是我的代码;它可以完美地删除非嵌套属性(例如,customerName),但如果我尝试在嵌套属性(如address.street3)中使用它,它会无声地失败。

const params = {
    TableName: customerTable,
    Key: {
        customerId: customerId,
    },
    AttributeUpdates: {
        'address.street3':
        {
            Action: 'DELETE'
        }
    }
};

dynamoDb.update(params, function (err, data) {
    if (err) {
        console.error("Unable to update customer. Error JSON:", JSON.stringify(err, null, 2));
    }
    else {
        console.log("UpdateCustomer succeeded:", JSON.stringify(data.Attributes));
        responseHelper.ResponseHelper.success(JSON.stringify(data.Attributes), 200, callback);
    }
});

我可以删除嵌套属性address.street3

1 个答案:

答案 0 :(得分:8)

以下是删除" address.street3"的代码。属性。

var docClient = new AWS.DynamoDB.DocumentClient();

var params = {
        TableName : "customer",
        Key : {
            "customerId": "customer_001"            
        },
        UpdateExpression : "REMOVE address.street3",
        ReturnValues : "UPDATED_NEW"
    };

console.log("Updating the item...");
docClient.update(params, function(err, data) {
    if (err) {
        console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("UpdateItem succeeded:", JSON.stringify(data));
    }
});