AliMohammed Manzoor
AliMohammed Manzoor

Reputation: 147

How to delete a map from list in an Item of a table in DynamoDB using nodeJS?

I am developing an app and I am using AWS DynamoDB as the database server. DB is connected with nodeJS. I have a table called chat to store messages. In chat table every item have connectionid as partition key. An item looks like this

{"id": "123456", "messages": [{"id": "1214545","message":"ksadfksfmsfsdf","sender":"112415", "timestamp": "27:1:2022:11:53"}]}

This is a single Item and there are more inside the chat table.

I have been trying to delete a message inside the item. Its easy for me to delete a whole item because connectionid is the partition key. But I want to delete it with respect to id which is nested. How to achieve this?

Upvotes: 0

Views: 170

Answers (1)

Jatin Mehrotra
Jatin Mehrotra

Reputation: 11496

But I want to delete with respect to id which is nested. How to to achieve this?

No, you cannot. Its not possible to have nested primary key or secondary index on nested key.

I would recommend to remodel your table schema, you can use a composite key for example:

  • keep message array as same, just take id out of the nesting.
  • you can have "id": "123456" as sort key and "id": "1214545" as primary key.
  • a combination of both will give you the ability to find the unique record and delete it.
  • if you want to delete all messages ( multiple messages), you can delete it using query operation with key = "id": "1214545" which is your primary key.

Or a very simple solution would be to have message id as your single primary key if it is going to be unique for all record.

Note:- in composite key primary key can have same value but sort key should be different all the time. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html#HowItWorks.CoreComponents.PrimaryKey

Upvotes: 2

Related Questions