Spring Boot 2.3.0 - MongoDB Library does not create indexes automatically
Asked Answered
K

2

21

I've provided a sample project to elucidate this problem: https://github.com/nmarquesantos/spring-mongodb-reactive-indexes

According to the spring mongo db documentation (https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mapping-usage):

the @Indexed annotation tells the mapping framework to call createIndex(…) on that property of your document, making searches faster. Automatic index creation is only done for types annotated with @Document.

In my Player class, we can observe the both the @Document and @Indexed annotation:

@Document
public class Player {

@Id
private String id;

private String playerName;

@Indexed(name = "player_nickname_index", unique = true)
private String nickname;


public Player(String playerName, String nickname) {
    this.id = UUID.randomUUID().toString();
    this.playerName = playerName;
    this.nickname = nickname;
}

public String getPlayerName() {
    return playerName;
}

public void setPlayerName(String playerName) {
    this.playerName = playerName;
}

public String getNickname() {
    return nickname;
}

public void setNickname(String nickname) {
    this.nickname = nickname;
}
}`

And in my application class, i'm inserting oneelement to check the database is populated successfully:

@PostConstruct
public void seedData() {
    var player = new Player("Cristiano Ronaldo", "CR7");

    playerRepository.save(player).subscribe();

}

If I check MongoDb after running my application, I can see the collection and the element created successfully.

The unique index for nickname is not created. I can only see an index created for the @Id attribute. Am I missing anything? Did I mis-interpret the documentation?

Knavish answered 5/6, 2020 at 5:25 Comment(3)
did you try to set index name? @Indexed(name = "nick_name_index"). Also did you try to save duplicates to test index?Guesthouse
yep duplicates are saved and yes I have tried with name, no difference. same issues. everytime I restart the app, a new entry is added with the same values.Knavish
I've updated my sample with a name added to the index.Knavish
S
39

The Spring Data MongoDB version come with Spring Boot 2.3.0.RELEASE is 3.0.0.RELEASE. Since Spring Data MongoDB 3.0, the auto-index creation is disabled by default.

To enable auto-index creation, set spring.data.mongodb.auto-index-creation = true or if you have custom Mongo configuration, override the method autoIndexCreation

@Configuration
public class CustomMongoConfig extends AbstractMongoClientConfiguration {

  @Override
  public boolean autoIndexCreation() {
    return true;
  }

  // your other configuration
}
Sciuroid answered 5/6, 2020 at 14:2 Comment(1)
Extending AbstractMongoClientConfiguration like this doesn't work anymore as you now also have to override the getDatabaseName() method. The property spring.data.mongodb.auto-index-creation however does still work.Sara
I
0

I've faced this problem when upgrading the spring boot version to 2.3.x and overriding this method on the config class solved it (what @yejianfengblue said above)

  @Override
  public boolean autoIndexCreation() {
    return true;
  }
Imf answered 7/9, 2020 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.