I have the following Java POJO
in my spring boot application:
public class Round {
private ObjectId _id;
@NotEmpty
@Getter
@Setter
@Accessors(fluent = true)
@JsonProperty("userId")
private String userId;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
@Getter
@Setter
@Accessors(fluent = true)
@JsonProperty("date")
private LocalDate date;
@Min(value = 1, message = "Score should not be less than 1")
@Getter
@Setter
@Accessors(fluent = true)
@JsonProperty("score")
private int score;
// rest of fields
}
I have the following MongoRepository
:
@Repository
public interface RoundRepository extends MongoRepository<Round, String> {
List<Round> findByUserId(String userId);
@Query("{'userId' : ?0 , '_id' : ?1}")
Optional<Round> findByUserIdAnd_id(String userId, ObjectId _id);
}
In my Service class I have a working create()
and I am trying to implement an update()
method to be used with the PUT
mapping in my controller:
public Round create(String userId, @Valid @NotNull @RequestBody Round round) {
// set userId from path
round.userId(userId);
roundRepository.save(round);
return round;
}
public void put(String userId, ObjectId objectId, Round updatedRound) {
// get original round
Optional<Round> round = roundRepository.findByUserIdAnd_id(userId, objectId);
//todo - how to implement PUT in mongo to update to "updatedRound"?
}
I am relatively new to MongoDB
, is there a set way to do this? I.e. keep the same ObjectId
etc but update other fields in the document?