简体   繁体   English

如何使用 Nodejs 从 aws Dynamodb 中的地图中删除项目

[英]How to delete an item from a Map in aws Dynamodb using Nodejs

I have a table in Dynamo db called Settings the unique keys is email and has an attribute called display_names this is type Map and has the following structure {'123-456': "any name", '789-123': 'another name'}我在 Dynamo db 中有一个名为Settings的表,唯一键是email并且有一个名为display_names的属性,这是类型 Map 并且具有以下结构{'123-456': "any name", '789-123': 'another name'}

I'm trying to delete the name at with the attribute '123-456' with the following params我正在尝试使用以下参数删除属性为“123-456”的名称

TableName: 'Settings',
        Key: {
            email: 'user@test.com',
        },
        UpdateExpression: 'DELETE display_names :p',
        ExpressionAttributeValues: '{":p" : {"S": 123-456}}'
    };

But when I run the code I get the following error ExpressionAttributeValues contains invalid key: Syntax error; key: \\"11\\"",但是当我运行代码时,我收到以下错误ExpressionAttributeValues contains invalid key: Syntax error; key: \\"11\\"", ExpressionAttributeValues contains invalid key: Syntax error; key: \\"11\\"",

I hope you can help me, thank you in advance!我希望你能帮助我,在此先感谢你!

You can use below UpdateExpression to remove a key from the map.您可以使用下面的UpdateExpression从地图中删除一个键。

const keyToDelete = '123-456';
const params = {
  TableName: 'Settings',
  Key: {
    email: 'user@test.com',
  },
  UpdateExpression: `REMOVE #parent.#key`, // Here. Note: use `` instead of '',
  ExpressionAttributeNames: {  
    "#parent": 'display_names',
    "#key": keyToDelete
  },  
  ReturnValues: 'ALL_NEW'
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM