Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamodb query - OR condition in KeyConditionExpression

Tags:

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);
like image 374
Arun SS Avatar asked Aug 19 '15 15:08

Arun SS


People also ask

Does Amazon DynamoDB support conditional operations?

Yes, like all the other database management systems, DynamoDB also supports all the conditional operators, User can specify a condition that is satisfied for a put, update, or delete operation to work on an item.

What is Keyconditionexpression DynamoDB?

This allows Query to retrieve one item with a given partition key value and sort key value, or several items that have the same partition key value but different sort key values. from the docs. It is not supported by DynamoDB and it is actually to save your money.

What is key condition expression?

Key condition expressions for query. To specify the search criteria, you use a key condition expression—a string that determines the items to be read from the table or index. You must specify the partition key name and value as an equality condition. You cannot use a non-key attribute in a Key Condition Expression.

Does DynamoDB support complex queries?

DynamoDB does support the complex condition on FilterExpression . Perfectly fine.

What are condition keys in DynamoDB?

You can review the following two examples of the condition keys − dynamodb:LeadingKeys − It prevents the item access by users without an ID matching the partition key value. dynamodb:Attributes − It prevents users from accessing or operating on attributes outside of those listed.

How to query a compound primary key in DynamoDB?

Remember the basic rules for querying in DynamoDB: The query includes a key condition and filter expression. The key condition selects the partition key and, optionally, a sort key. The partition key query can only be equals to (=). Thus, if you want a compound primary key, then add a sort key so you can use other operators than strict equality.

How to use the keyconditionexpression parameter in a query?

A query utilizes the KeyConditionExpression parameters to select items, which requires providing the partition key name and value in the form of an equality condition. You also have the option to provide an additional condition for any sort keys present. A few examples of the sort key conditions are −

What is a query operation in Amazon DynamoDB?

The Query operation in Amazon DynamoDB finds items based on primary key values. You can query any table or secondary index that has a composite primary key (a partition key and a sort key). You must provide the name of the partition key attribute and a single value for that attribute.


2 Answers

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

like image 91
user1697575 Avatar answered Sep 18 '22 11:09

user1697575


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.

like image 37
Raymond Lin Avatar answered Sep 20 '22 11:09

Raymond Lin