databaseamazon-web-servicesnosqlamazon-dynamodb

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


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?


Solution

  • 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'
    }