删除dynamodb中的嵌套属性 [英] Remove nested attribute in dynamodb

查看:229
本文介绍了删除dynamodb中的嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在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"
  }
}

我需要删除嵌套属性 address .street3 。我该如何实现呢?

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

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

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

我该如何删除嵌套属性 address.street3

What can I remove the nested attribute address.street3?

推荐答案

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

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

这篇关于删除dynamodb中的嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆