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);
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.
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.
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.
DynamoDB does support the complex condition on FilterExpression . Perfectly fine.
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.
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.
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 −
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With