Hi there how do I do the following in one call:
- update “updated_at” field if the document exists based on query
- insert a POJO if the document does NOT exist based on query
- return the action performed, insert vs update
- return the document created/updated
Thanks a bunch
This is what I currently have
public Match createMatchIffNotExist2(Match m) {
//this upsert is needed because we need to create a new match if there isn't an active match between the users
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().upsert(true).returnDocument(AFTER);
Bson andFilter = and(
eq(CommonStrings.MATCH_ORDER_0_USR_ID_FIELD, m.getOrder0UserId()),
eq(CommonStrings.MATCH_ORDER_1_USR_ID_FIELD, m.getOrder1UserId()),
eq(CommonStrings.MATCH_IS_ACTIVE_FIELD, m.getIsActive())
);
List<Bson> updates = new ArrayList<>(Arrays.asList(
Updates.set(UPDATED_AT_FIELD, CommonUtils.getNowBson()),
Updates.setOnInsert(MATCH_IS_ACTIVE_FIELD, m.getIsActive()),
Updates.setOnInsert(MATCH_FIELD_1, m.getMatchField1()),
Updates.setOnInsert(MATCH_FIELD_2, m.getMatchField2()),
....
);
return matchCollection.findOneAndUpdate(andFilter, updates ,options);
}
My questions are:
- could I do everything in 1 call?
- how I could get #3 mentioned above?
- How do I turn Match to into Bson object where if I add attribute to Match object, I dont have to keep adding on setOnInsert statement