How to check whether an attribute is not present in dynamoDB filter expression

狂风中的少年 提交于 2021-01-29 22:39:51

问题


I am introducing a new field InActive in my dynamo DB table, now I want to fetch the records which were active earlier (assuming all previous records were active) along with new Active records.

What should be my dynamo DB Query expression?

final HashMap<String, AttributeValue> activeExpressionAttrValue = new HashMap<>();
    activeExpressionAttrValue.put(":in_active_false", new AttributeValue().withBOOL(false));
    activeExpressionAttrValue.put(":in_active_null", new AttributeValue().withNULL(true));


 dynamoDBQueryExpression.withHashKeyValues(ddbRecord.builder().primarykey(p).build())
            .withFilterExpression(ddbRecord.IN_ACTIVE + " = :in_active_false OR" +
                    ddbRecord.IN_ACTIVE + " = :in_active_null")
            .withExpressionAttributeValues(activeExpressionAttrValue);

Is this correct?


回答1:


see - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ConditionExpressions.html

 ... ddbRecord.IN_ACTIVE + " = :in_active OR attribute_not_exists(" + ddbRecord.IN_ACTIVE + ")" ...

DynamoDb doesn't perform implicit conversion to null. For above query to work the data in table would have to contain:

 {
     "some_attribute": {
          "S": "some attribute string value"
     },
     "is_active": {
          "NULL":true
     }
 }

However, I suspect, that old records in table simply do not contain "is_active" attribute rather than having it present and set to NULL hence valid condition is attribute_not_exists(is_active)



来源:https://stackoverflow.com/questions/61209795/how-to-check-whether-an-attribute-is-not-present-in-dynamodb-filter-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!