2

I'm using DynamoDb v2 interface for boto to make counter increments in my table. (I need v2 interface as I'll be dealing with indexes later on)

Somehow I'm not able to find how to do that without fetching item & updating it again.

Here is the code I'm using

from boto.dynamodb2.table import Table
from boto.dynamodb2.items import Item

my_table = Table('my-table')

# Update counter for existing record.
data = {'key': 'my_key',
        'range_key': 'my_range',
       }

item = Item(my_table, data)
#### Do something here to increment 'counter' by 1
item.save()

What should I do to increment a 'counter' field??

1 Answer 1

6

Check out this answer: Update DynamoDB Atomic Counter with Python / Boto

This is dealing with DynamoDB v1, and I have not tested if this still works with v2.

If you are already fetching the current value, it appears that

item.add_attribute('counter', 1)
item.save()

will do an update_item request.

You can also do an update_item request directly:

dynoConnLayer1.update_item("my_table", 
                    {"key":{"S":"my_key"}, 'range_key' : {"S": "my_range"}},
                    {"counter":
                        {"Action":"ADD","Value":{"N":"1"}}
                    }
                )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.