简体   繁体   中英

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

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM