Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot 2.3.0 - MongoDB Library does not create indexes automatically

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?

like image 256
nmmsantos Avatar asked Jun 05 '20 05:06

nmmsantos


People also ask

Does MongoDB automatically create indexes?

MongoDB automatically determines whether to create a multikey index if the indexed field contains an array value; you do not need to explicitly specify the multikey type.

Does spring boot provide MongoDB auto configuration?

Spring Boot provides auto-configuration for Redis, MongoDB, Elasticsearch, Solr and Cassandra; you can make use of the other projects, but you will need to configure them yourself.

How is indexing implemented in MongoDB?

Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect.

Which is better MongoTemplate or MongoRepository?

So I'd say that MongoTemplate is a better option, unless you have a very elaborated POJO model or need the custom queries capabilities of MongoRepository for some reason. Good points/examples. However your race condition example and undesired result can be avoided using @Version to prevent that very scenario.

Why can’t I create an index in spring data MongoDB?

This is because, as of Spring Data MongoDB 3.0, automatic index creation is turned off by default. We can, however, change that behavior by explicitly overriding autoIndexCreation () method in our MongoConfig:

Why use Spring Boot with MongoDB?

With Spring boot, we can quickly create stand-alone applications without having to make too many configuration changes (as we will see later). MongoDB is the most popular NoSQL database because of the ease with which data can be stored and retrieved.

What is the default name of an index in MongoDB?

MongoDB indexes use a B-tree data structure. The default name for an index is the concatenation of the indexed keys and each key's direction in the index ( i.e. 1 or -1) using underscores as a separator. For example, an index created on { item : 1, quantity: -1 } has the name item_1_quantity_-1.

How do I add indexes to my MongoDB collections?

To add some indexes to your collections, you could run some functions directly via the Mongo Shell — or Spring Data can be used to handle it for you. As the title suggests, that's what we will be looking into in this post. Let's start with some background information about why we should use indexes.


1 Answers

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
}
like image 116
yejianfengblue Avatar answered Oct 17 '22 21:10

yejianfengblue