In my Spring MVC application I have the following Mongo entities:
@Document(collection = "users")
public class UserMongoEntity {
@Id
private String id;
@NotBlank
@Email
@Indexed(unique = true, sparse = true)
private String email;
private String firstName;
and a Submission entity containing User as a field:
@Document(collection = "submissions")
public class SubmissionMongoEntity {
@Id
private String id;
private Instant timestamp;
@Valid
private UserMongoEntity user;
When I make a request to insert a new User, I get an error if a user with the same email already exists, as expected. However, when I insert a submission with the same user twice, I get an exception for violating the uniqueness of the index: duplicate key error collection: user.email dup key
.
I could remove the @Indexed
, but I believe there is a better way.