@Indexed for an attached property not working in Spring -data for mongo

I have the following object structure:

@Document(collection = "user")
@TypeAlias("user")
public class User {
    @Id
    private ObjectId id;
    private Contact info = new Contact();
}

and here is the contact field:

public class Contact {
    @Indexed(unique = true)
    private String mail;
}

But for some reason unknown to me, I don't see Spring -data creating a unique index for the info.mail property

To summarize, I have this json structure of the user object: {_ ID: xxxxx, information: {mail: " abc@xyz.shoes "}}

And I want to create a unique index on info.mail using Spring data with the above pojo structure. Please, help.

+4
source share
2 answers

, @Indexed . @CompoundIndex - :

@Document(collection = "user")
@TypeAlias("user")
@CompoundIndexes({
    @CompoundIndex(name = "contact_email", def = "{ 'contact.mail': 1 }", unique = true)
})
public class User {
    @Id
    private ObjectId id;
    private Contact info = new Contact();
}
+14

, , Contact @Document i.e.

@Document
public class Contact {
    @Indexed(unique = true)
    private String mail;
}

, spring mongodb

, @Document.

+2

Source: https://habr.com/ru/post/1530966/


All Articles