Search code examples
amazon-dynamodbdynamodb-queries

Remove items from list attribute conditionally


I have an item with a list attribute, each element of the list is a map.

"listAttr": [
    {"field1":"value1", "field2":"value2", ...}
    , {"field1":"value3", "field2":"value4", ...}
    ....
]

I want to execute a REMOVE statement that would look like this:

REMOVE listAttr[x]

With a condition that

listAttr[x].field1 = :val

I want to remove all element of the list that match my condition

I don't want to query the item first to calculate the ids, because it would create a race between insert and remove (id could get out of sync by the time I execute the REMOVE)

The SQL equivalent would be:

DELETE from listAttr where field1=?

How do I do this ?


Solution

  • My understanding is that this isn't possible in DynamoDB. You can use a PUT operation with optimistic locking to avoid lost updates.

    If there are frequents writes to the item which would make this operation infeasible then you could look into "vertical partitioning" where you split up a single large item into multiple items, this allows you to reduce the risk of race conditions, in case applications only change parts of the item at a time, and potentially reduces the costs of reads or writes, in case the single item is multiple kb large and you can have more targeted queries.