In a Spring boot + MongoDB application, I'm trying to create an unique index for an email field.
@Document
public class User {
@Id
private String id;
@Indexed(unique = true)
private String email;
}
public interface UserRepository extends MongoRepository<User, String>
But I'm still able to insert two user objects with the same email, so
userRepository.save(new User("my@email.com"))
userRepository.save(new User("my@email.com"))
creates two entries in the user collection.
What am I doing wrong?
I'm aware of Spring Data MongoDB - Where to create an index programmatically for a Mongo collection?, but I'm looking for a "annotation-only" solution.