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?
DynamoDB does support the complex condition on FilterExpression . Perfectly fine.
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.
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.
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.
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' }
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