I have a DynamoDB table where each item in the table has an array of comments as an attribute. My schema looks something like this:
{
"id": "abc",
"name": "This is an item"
"comments": [
"commentId": "abcdefg",
"commentAuthor": "Alice",
"commentDetails": "This item is my favourite!"
]
}
I want to be able to edit an individual comment by its commentId
, but it's not clear how to write a DynamoDB expression for this update (I'm using DocumentClient).
How can we update an entry in an embedded array in Dynamo? Is it possible to update by array index, or by a query expression?
To update an existing item in an Amazon DynamoDB table, you use the UpdateItem operation. You must provide the key of the item that you want to update. You must also provide an update expression, indicating the attributes that you want to modify and the values that you want to assign to them.
Can we update the sort key in DynamoDB? No, you can not update the sort key after the table is provisioned. However, you can create a new table and put the existing data in the newly created table, and delete the old table.
We can store the values in DynamoDb in 2 ways, (i) In an RDBMS Type of Structure for the DynamoDB, we can add a new Coulmn by executing the same command keeping the "new Column" entry within which the Records in the Existing Table has been created.
I think there should be some modification to your data model.You should have used dynamodb map for this just make your commentId as a key to access each comment
{
"id": "abc",
"name": "This is an item"
"comments": {
"abcdefg":{
"commentAuthor": "Alice",
"commentDetails": "This item is my favourite!"
}
}
}
Then the query would look like this and you must know two things to update the comment 1 is Item id and 2 is comment id
var params = {
TableName: Tablename,
Key: {id: "abc"},
UpdateExpression:"set comments.#abcdefg.commentDetails=:val1",
ExpressionAttributeNames: {
"#abcdefg":"abcdefg"
},
ExpressionAttributeValues:{
":val1":"New comment text"
}
}
db.update(param,callback);
Please Let me know and pardon me,explain your question if i misunderstood as i did not have enough points to clear my doubts in comment.
Personally, I would not put items I want to edit or access frequently this far away from the primary key. Remember DynamoDB is basically a key-value store, so whenever you want to access stuff it's always better to have those items exposed through primary keys.
So what could you do about this, in my opinion, the best way is to slightly alter your data model. So I would create a table with a primary key ID and a sort key of the item type. In your case, you would now have two item types, "Item", and "comment_[commentID]". Each comment could have the author and details fields as its attributes so something like this:
- [ID - ITEMTYPE] - NAME
- "abc" - "item" - "This is an item"
- [ID - ITEMTYPE] - AUTHOR - DETAILS
- "abc" - "comment_abcdefg" - "Alice" - "This item is my favourite!"
Now if you want to get all comments for an item you simply query for the item ID with the sort key starts with "comment". If you want to edit a comment you could simply get this item by adding the ID to the sort key which would result in an exact match. (also this would be VERY fast). There are several ways in which you could make this even more flexible but this should be enough to do what you asked for.
So sorry I just read your comment on the other answer, of course, you can add a sorting number in between the comment and the ID field in the item type sortkey this way the comments would sort automatically in that order(or reverse off course).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With