DynamoDB Upsert with condition on numeric attribute


Is it possible to do an upsert (insert if the item does not exist, update otherwise) with a condition on a specific attribute using the Update() call?

For example I have a UpdatedTime that I only want to allow to be greater when upserting, otherwise do nothing. This is fine if the item already exists, but the conditional will fail if there is no existing item. Is it possible to do this with a single call to DynamoDB? The alternative is to check the error and then do an Insert(), but now I have a race to deal with.

asked a year ago372 views
1 Answer

To answer my own question (not sure why it was not obvious, maybe writing it down helped...):

The conditional needs to be (1) attribute_not_exists OR the (2) <= condition on the numeric field.

On the first UpdateItem() call, condition (1) is true and (2) is always false. On subsequent calls, (1) is false, but (2) can potentially be true.

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions