Reputation: 111
I am trying to remove a value from a List/array in DynamoDB.
I understand I need to use REMOVE which requires the index of the value to remove.
Is it possible to find the index of a given value and remove the value in a single updateItem?
If not, what is the best way to perform this action?
Much appreciated.
Upvotes: 2
Views: 3015
Reputation: 4456
Looks like you can't do such atomic removes from a list in DynamoDB. So you are right, to remove an item from a list, you have to find the index of the item first, it's impossible to do both steps in a single UpdateItem operation. You read the item first, and then perform the update.
Such atomic value remove actions as you are looking for, are possible only on sets: NS (number set), SS (string set), BS (binary set).
When using DELETE action in UpdateItem, you have to specify a list of values to remove, you don't have to find their indexes before performing the action (actually, set is an unordered collection, so elements do not have indexes)
So if your use case allows using a set instead of list, I would suggest doing so. (The differences between set and list are: a set holds unordered, unique elements of the same type, while list is an ordered collection of values with no restrictions on the data types)
Upvotes: 3