Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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??

share|improve this question
add comment

1 Answer

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"}}
                    }
                )
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.