2017-07-18 66 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