Is it possible to do a conditional write/update using DynamoDB mapper when you want to have condition like: Write if attribute is not equal to X? I want to do a conditional write that does something like this:
DynamoRecord record = mapper.load(DynamoRecord.class, hashKey);
if (record.getSomeAttr() != terminationValue) {
mapper.save(newRecord);
}
The attribute always exists. It can have multiple values and represents a condition after which the updates to the record should stop.
I read this article and AWS documentation and a bunch of other posts but seems like only == operator is supported along with exists check. Is it possible to do a != conditional write using a mapper? If so, any pointers are much appreciated.
Thanks.
Summary from @notionquest's answer
ComparisonOperator.NE@DynamoDBNativeBoolean for correct results while using ExpectedAttributeValue(new AttributeValue().withBool(true)). See here for details.Please use "withComparisonOperator(ComparisonOperator.NE)" for NOTEQUALS condition. Refer the below example.
DynamoDBSaveExpression saveExpression = new DynamoDBSaveExpression();
Map expected = new HashMap();
expected.put("Status",
new ExpectedAttributeValue(new AttributeValue(status)).withComparisonOperator(ComparisonOperator.NE));
saveExpression.setExpected(expected);
dynamoDBMapper.save(obj, saveExpression);
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