Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no **not equal** comparison in DynamoDB queries?

Tags:

I try to query my table Tinpon with a secondary index yielding a partition-key category and sort-key tinponId. My goal is to exclude items with certain tinponIds. My first thought would be to make a negative compare: keyConditionExpression = "category = :category AND tinponId != :tinponId" but there is only a equal = comparison. Then I tried serval other methods (with sadly do not exist): keyConditionExpression = "category = :category NOT tinponId = :tinponId" keyConditionExpression = "category = :category AND tinponId <> :tinponId" keyConditionExpression = "category = :category AND tinponId < :tinponId AND tinponId > :tinponId" Following the AWS guide there is no not equal comparisson. Why so? And is there a way to query DynamoDB excluding a list of ids or is the only option to retrieve a whole bunch of items and filter them later manually?

like image 553
DrDirk Avatar asked Jul 09 '17 15:07

DrDirk


People also ask

Does DynamoDB support complex queries?

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

Is DynamoDB good for querying?

Querying is a very powerful operation in DynamoDB. It allows you to select multiple Items that have the same partition ("HASH") key but different sort ("RANGE") keys.

How does DynamoDB Query work?

In a Query operation, DynamoDB retrieves the items in sorted order, and then processes the items using KeyConditionExpression and any FilterExpression that might be present. Only then are the Query results sent back to the client. A Query operation always returns a result set.

What is KeyConditionExpression in DynamoDB?

Optionally, you can provide a sort key attribute and use a comparison operator to refine the search results. Use the KeyConditionExpression parameter to provide a specific value for the partition key. The Query operation will return all of the items from the table or index with that partition key value.


1 Answers

The KeyConditionExpression doesn't allow not equals for the sort key. However, you can use the "Not Equals i.e. <>" in FilterExpression.

KeyConditionExpression : 'category = :category',     FilterExpression : 'tinponId  <> :tinponIdVal', ExpressionAttributeValues : {     ':category' : 'somevalue',     ':tinponIdVal' :  'somevalue' } 
like image 196
notionquest Avatar answered Sep 20 '22 08:09

notionquest