Update one and just one document in DynamoDB

0

I'm creating a Call Center system and I would like to know how to update one and just one document in a DynamoDB table in order to avoid making duplicated calls, let's say I will load a pool of numbers available to call and I need to mark them when an agent take one of the phonenumbers.

Armand
asked 3 months ago61 views
2 Answers
3

For me, I would perhaps use a secondary index for this. Lets imagine each of the items in the pool are a separate item and your table looks like this:

CallNoDateOtherisTakenUser
12342022-09-01T00:00DataNo
98762022-09-02T16:00DataJeff
56782022-09-01T10:00DataNo
45672022-09-01T12:00DataLee

No, each of the available calls have the attribute isTaken with a value of "No". So then we have a Global Secondary Index which is sparse in nature, and we decide the partition key for that is the isTaken column while the sort key is Date, in which only items from the table with a value for isTaken will be stored in the index, ordered by the date:

isTakenDateCallNoOther
No2022-09-01T00:001234Data
No2022-09-01T10:005678Data

So now you can see the only items in the index are the calls which are in the Available queue. So when an operator wishes to retrieve an available call, they do a Query operation on the index defining a partition key of No and a Limit=1, in which they will retrieve the oldest item in the waiting queue:

aws dynamodb query \
--table-name test1 \
--index-name available \
--key-condition-expression "isTaken = :val" \
--expression-attribute-values  '{":val":{"S":"No"}}' \
--limit 1
isTakenDateCallNoOther
No2022-09-01T00:001234Data

Once an operator receives their item, they must lock it to themselves by adding their User name to it and removing the isTaken value to remove it from the available queue:

aws dynamodb update-item \
--table-name test1 \
--key '{"CallNo": {"N": "1234"}}' \
--update-expression "SET #attr_one = :attr_val REMOVE #attr_two " \
--expression-attribute-names '{"#attr_one":"User", "#attr_two: "isTaken"}' \
--expression-attribute-values '{":attr_one":{"S": "Lee"}}' \
--condition-expression "attribute_exists(#attr_two)"

The update will remove the item from the GSI by removing the value for isTaken, it uses a condition expression only to update that item if isTaken already exists, if it evaluates to false, the update will fail which means another user has already taken that call and you have to poll the available queue for the next available call.

After this, your table will look like below:

CallNoDateOtherisTakenUser
12342022-09-01T00:00DataLee
98762022-09-02T16:00DataJeff
56782022-09-01T10:00DataNo
45672022-09-01T12:00DataLee

And the Available index:

isTakenDateCallNoOther
No2022-09-01T10:005678Data
profile picture
answered 3 months ago
  • Thanks Leeroy, sounds very good!

  • You're welcome Armand, please feel free to accept the answer should it satisfy your use-case.

2

Hello - You can update (or perform general CRUD operations) on a single item by following this guide.

The TLDR is essentially:

DynamoDB provides four operations for basic create, read, update, and delete (CRUD) functionality. All these operations are atomic.

PutItem — Create an item.

GetItem — Read an item.

UpdateItem — Update an item.

DeleteItem — Delete an item.

Each of these operations requires that you specify the primary key of the item that you want to work with. For example, to read an item using GetItem, you must specify the partition key and sort key (if applicable) for that item.

profile picture
answered 3 months ago
  • Thanks, but being more specific: I need to update just one record by the condition status and not for the primary key. Based on this I could have several records, but I need to update only one with a code to know what agent has taken the record.

    Does make sense?

  • So you are adding a pool of numbers into an item? You'd need to use filter expressions - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.FilterExpression

    It might make more sense for each number to be it's own item though. It would make adding and querying attributes much simpler.

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