I have a complex native query which I want to convert to java DSL for future maintainability.
@Query("{'_id': ?0, 'lockStatus' : false}")
@Update(pipeline = {"{$set: { counter: {$add: [ '$counter', 1 ] }} }",
"{$set: { lockStatus: { $in: ['$counter', ?1 ]} }}",
"{$set: { lockUpdatedOn : ?2, 'metadata.updatedUuid': ?5, 'metadata.updatedTime': ?2}}",
"{ $set:{'completedDetail':{$cond:[{$eq:['$lockStatus',true]},{$concatArrays:[{$ifNull:['$completedDetail',[]]},"
+ "[{'card':?3, 'transactionId': 'NOT SET', 'transNo': '$counter', 'redeemStatus': false,"
+ " 'metadata': ?4 }]]},'$completedDetail']}}}"
})
Now the problem is I am not sure how to convert the $cond operation to DSL
I have tried something like this
Bson filter = Filters.and(Filters.eq(ID, generateCounterId(request)), Filters.eq(LOCK_STATUS, false));
List<Bson> updates = new ArrayList<>();
updates.add( Updates.inc(COUNTER, 1));
updates.add(Updates.set(LOCK_STATUS, Filters.in(COUNTER, getIWTransactionNumbers(request))));
updates.add(Updates.combine(
Updates.set(LOCK_UPDATED_ON, new Date()),
Updates.set(METADATA_UPDATED_UUID, updatedUuid),
Updates.set(METADATA_UPDATED_TIME, new Date())
));
// few more updates for the $cond operation
collection.updateOne(filter, updates);
Any help will be really appreciated