edA-qa mort-ora-y
edA-qa mort-ora-y

Reputation: 31851

How to query a dynamodb2 index using boto?

How can I query a dynamodb2 table with an index using boto? I'm not able to piece together enough information from the documentation or unit tests for boto.

I have a local index created as:

fields.KeysOnlyIndex('NameIndex', parts=[
    fields.HashKey('account_id', data_type='S'),
    fields.RangeKey('name', data_type='S'),
])

And would like to lookup an item using the account_id and name.

Attempting to make the call table.query( account_id__eq=account['id'], name__eq = name ) results in the error Query condition missed key schema element id.

Note: I would also prefer to avoid using the Table class and work directly with the connection.

Upvotes: 0

Views: 1082

Answers (1)

kukido
kukido

Reputation: 10601

with Table:

table = Table('accounts')
results = table.query(index='NameIndex', account_id__eq=account['id'], name__eq=name)

or with Connection:

results = conn.query(
    table_name='accounts',
    index_name='NameIndex', 
    select='ALL_PROJECTED_ATTRIBUTES', 
    key_conditions={
        'account_id': {
            'AttributeValueList': [
                {'S': account['id']},
            ],
            'ComparisonOperator': 'EQ',
        },
        'name': {
            'AttributeValueList': [
                {'S': name},
            ],
            'ComparisonOperator': 'EQ',
        },
    }
)

Upvotes: 1

Related Questions