2017-07-18 69 views
1

让我们说我在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?

问候

回答

2

这里是移除 “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)); 
    } 
}); 
+0

它的工作。谢谢 :) – wapt49