I have the below class as my document.
@Data
@Builder
@Document(collection = "test")
public class TestData {
@Id
private String id;
private String name;
@Indexed(unique = true)
private String hash;
}
Even if I'm using Indexed with unique enabled, I'm able to insert duplicate documents into collection. But if I generate index in mongo shell then it is working.
Is there any way where I can specify unique Index through code only?
error/exception
you are getting ? Please add some error/exception trace. – Microstructureunique = true
? – Dentate@Indexed
can't be applied at class level, It should be applied at field level. Use@CompoundIndex
instead. – Microstructurespring.data.mongodb.auto-index-creation=true
property change worked for me #53007318 – Cosby