2

My DynamoDB schema sample below:

{
 obj :  {
                 "count" : 3,
                 "id" : [ "X123", "X234", "X233"]
             }
}

New Id : ["X007"]

Now I need to update obj attribute by incrementing count & update new item in list. How to update it in single query ?

halfer
  • 19,824
  • 17
  • 99
  • 186
Error 404
  • 75
  • 2
  • 10

1 Answers1

2

You should use UpdateItem command with proper UpdateExpression, i.e. it should use SET and list_append to modify collection and ADD or SET to increment count.

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.SET.UpdatingListElements

Alexander Pavlov
  • 2,264
  • 18
  • 25
  • As per the above documentation "Use the SET action in an update expression to add one or more attributes to an item. If any of these attributes already exists, they are overwritten by the new values." In order to achieve this I need to know the value of list before final update – Error 404 Jan 10 '22 at 06:57
  • 1
    It will override only if you do not use `list_append`. You should merge old list and new item, something like `SET #my_list_attr=list_append(#my_list_attr, :new_item)` – Alexander Pavlov Jan 10 '22 at 13:07