Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove nested attribute in dynamodb

Let's say I have this item in DynamoDB:

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

I need to remove the nested attribute address.street3 from the item. How I can accomplish this?

Here is my code below; it works perfectly to remove non-nested attributes (for example, customerName), but if I try to use this in nested attributes (like address.street3), it silently fails.

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);
    }
});

What can I remove the nested attribute address.street3?

like image 358
wapt49 Avatar asked Jul 18 '17 14:07

wapt49


1 Answers

Here is the code to remove "address.street3" attribute.

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));
    }
});
like image 193
notionquest Avatar answered Oct 23 '22 02:10

notionquest