Reputation: 1013
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.Upvotes: 3
Views: 7101
Reputation: 59
DynamoDBSaveExpression saveExpression = new DynamoDBSaveExpression();
Map expected = new HashMap();
expected.put("Status",new ExpectedAttributeValue(new AttributeValue('NE')));
saveExpression.setExpected(expected);
dynamoDBMapper.save(obj, saveExpression);
Upvotes: -1
Reputation: 39176
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);
Upvotes: 5