Arun SS
Arun SS

Reputation: 1911

Dynamodb query - OR condition in KeyConditionExpression

I have a DynamoDB table with feed_guid as the global secondary index. I want to query with a set of feed_guid in that table. Since feed_guid is not my primary keys, I can't use getBatchItem. When I tried the following method, I got this error:

Invalid operator used in KeyConditionExpression: OR

    $options = array(
                'TableName' => 'feed',
                    'IndexName' => 'GuidIndex',
                    'KeyConditionExpression' => 'feed_guid = :v_guid1 or feed_guid = :v_guid2',

                    'ExpressionAttributeValues' =>  array (
                        ':v_guid1' => array('S' => '8a8106e48bdbe81bf88d611f4b2104b5'),
                        ':v_guid2' => array('S' => '19cab76242a6d85717de64fe4f8acbd4')
                    ),
                    'Select' => 'ALL_ATTRIBUTES',
                );
                $response = $dynamodbClient->query($options);

Upvotes: 27

Views: 69124

Answers (2)

user1697575
user1697575

Reputation: 2848

You can't use OR condition. You should use

rangeAttributeName BETWEEN :rangeval1 AND :rangeval2

if possible or

feed_guid IN (:v_guid1, :v_guid2)

See ExpressionAttributeValues and KeyConditionExpression

Upvotes: 12

Raymond Lin
Raymond Lin

Reputation: 491

In order to achieve what you want here, you'll need to take the union of two separate queries.

Currently, DynamoDB's Query API only supports having one condition on Hash AND Range Keys in only the KeyConditionExpression because this limits the items you search and ultimately reduces cost of say a more complex query like what you've described here.

Upvotes: 8

Related Questions