Felix Avelar
Felix Avelar

Reputation: 311

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'}

I'm trying to delete the name at with the attribute '123-456' with the following params

TableName: 'Settings',
        Key: {
            email: '[email protected]',
        },
        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\"",

I hope you can help me, thank you in advance!

Upvotes: 1

Views: 1204

Answers (1)

hoangdv
hoangdv

Reputation: 16157

You can use below UpdateExpression to remove a key from the map.

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

Upvotes: 1

Related Questions