2

I am reading official docs http://boto3.readthedocs.io/en/latest/_modules/boto3/dynamodb/conditions.html

You can create a condition like this:

Attr(key).eq(value)

Where you check if value equals to key's value using class Attr() with eq method.

But how do you use a SQL IN operator?

I would like to query if value IN key's value

At the docs it just appear an In class and I dont see any example or how to use it

class In(ComparisonCondition):
    expression_operator = 'IN'
    has_grouped_values = True

1 Answer 1

2

Operator IN cannot be used in KeyConditionExpression i.e. to get multiple hash key values using Query API. DynamoDB doesn't support this feature at the moment.

If you need to retrieve multiple hash key values, please use Batch Get Item api.

Sample code:-

email1 = "[email protected]"
email2 = "[email protected]"

try:
    response = dynamodb.batch_get_item(
    RequestItems={
        'users': {
            'Keys': [
                {
                    'email': email1
                },
                {
                    'email': email2
                },
            ],            
            'ConsistentRead': True            
        }
    },
    ReturnConsumedCapacity='TOTAL'
)
except ClientError as e:
    print(e.response['Error']['Message'])
else:
    item = response['Responses']
    print("BatchGetItem succeeded:")
    print(json.dumps(item, indent=4, cls=DecimalEncoder))
Sign up to request clarification or add additional context in comments.

3 Comments

Why is in included for boto3.dynamodb.conditions but DynamoDB does not support it yet?
It seems to me, from docs.aws.amazon.com/amazondynamodb/latest/developerguide/…, that the "in" operator is support, and should be supported for example in a Scan (in its FilterExpression, not KeyConditions). If not, why not?
@notionquest is saying "Operator IN cannot be used in KeyConditionExpression" - not that it can't be used at all. Dynamo can't use it to find items, however, it can be used to filter items. So, if you wanted, you could use it in FilterExpression or other forms of filtering.

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.