问题
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