Table of Contents
Overview
If you have tried setting auto-index-creation
in spring data mongodb without success, like this:
data: mongodb: host: ${MONGO_HOST:dummy-host} port: ${MONGO_PORT:27017} database: ${MONGO_DB:dummy-db} username: ${MONGO_USER:dummy-user} password: ${MONGO_PASSWORD:dummy-password} authentication-database: admin auto-index-creation: true
Then this post will show you how to work around this.
Let’s get started.
Enable auto index creation in MongoDB
First, let’s consider this document
import io.ukata.api.core.models.AbstractDocument; import io.ukata.api.core.models.dto.RateLimitWindow; import lombok.Getter; import lombok.Setter; import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.Document; import java.time.temporal.ChronoUnit; import java.util.Map; @Getter @Setter @Document("rate_limit_config") public class RateLimitConfig extends AbstractDocument { @Indexed(name = "rate_limit_config_service_name", unique = true) private String serviceName; private Map<String, RateLimitWindow> limits; //subscriptions and their limits private RateLimitWindow freeUserLimits = new RateLimitWindow(ChronoUnit.HOURS, 2); }
Here, I defined an unique index for my document named rate_limit_config_service_name
but when the application start, I didn’t see my index.
The solution is to create a configuration bean like this:
import com.mongodb.ConnectionString; import com.mongodb.MongoClientSettings; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.MongoDatabaseFactory; import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory; import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.lang.NonNull; @Configuration public class MongoDBConfig extends AbstractMongoClientConfiguration { @Value("${spring.data.mongodb.database:ukata}") private String databaseName; @Value("${spring.data.mongodb.uri:}") private String uri; @Override protected boolean autoIndexCreation() { return true; } @NonNull @Override protected String getDatabaseName() { return databaseName; } @NonNull @Override public MongoClient mongoClient() { ConnectionString connectionString = new ConnectionString(uri); MongoClientSettings mongoClientSettings = MongoClientSettings.builder() .applyConnectionString(connectionString) .build(); return MongoClients.create(mongoClientSettings); } }
Make sure you configure your connection URI correctly.
And that’s it! Now your application should create the index for you.
Conclusion
In this post, I’ve shown you how to enable auto index creation in Spring Data Mongodb
I build softwares that solve problems. I also love writing/documenting things I learn/want to learn.