5

I know there are similar questions like this one: Spring boot / mongo wont create index with the index annotation

Also issues in Github like 'spring.data.mongodb.auto-index-creation=true' not working

And also I've tried this Baeldung guide: Spring Data MongoDB – Indexes, Annotations and Converters

So the problem is I'm trying to add an index to an existing collection using @Indexed anotation like this:

@CreatedDate
@Indexed(name="timestamp_index", expireAfterSeconds=100))
private Date timestamp;

The field is a timestamp which is created automatically when an element is inserted into the DB.

Also the class has @Document tag.

So, what have I tried?

Following other answers first thing I did is to add spring.data.mongodb.auto-index-creation: true in this way:

spring:
  data:
    mongodb:
      uri: ${env.mongo-database.url}
      auto-index-creation: true

This not works... but I've also read that the problem can be if I have a AbstractMongoClientConfiguration class.

Currently the project doesn't have that class, but exists a MongoConfiguration class with the @Configuration tag. I don't know if this can interfere or something.

The class is like this:

@Configuration
public class MongoConfiguration { /*creates some beans*/ }

This class create a @Bean named mongodb so I've tried to add manually auto-index to true here:

@Primary
@Bean(name = "mongodb")
@ConfigurationProperties(prefix = "spring.data.mongodb")
public MongoProperties getMongodbProperties() {
    MongoProperties mongoProperties = new MongoProperties();
    mongoProperties.setAutoIndexCreation(true);
    return mongoProperties;
}

But this doesn't work either. The index is not created.

Also this class doesn't extends from AbstractMongoClientConfiguration so I can't override the method autoIndexCreation

Also I can create the index programantically with this code:

mongoTemplate
    .indexOps(PagoDto.class)
    .ensureIndex(new Index().on("timestamp", Sort.Direction.ASC).expire(100));

But for clearer implementation I'd like to do using only annotations in the model.

So the point is: Neither use auto-index-creation in application properties nor trying to add the value into properties works.

Maybe the configuration class doesn't allow to create automatically? It interferes in any way?

I'm not supposed to be authorized to modify the configuration class, if is a little change like call setAutoIndexCreation method there is no problem but I can't change the logic and extends from AbstractMongoClientConfiguration. So the ideal scenario is to get it to work with the annotation @Indexed.

Thanks in advance

2
  • 1
    you wrote “add an index to an existing collection” -> I'm afraid it's impossible to do that only by annotation and you have to do that programmatically. I had some similar case, I had @Indexed with some value x for the expiry seconds, and once I wanted to change that value x to y, I wasn’t able to change it just in the annotation, and I had to drop the already exist index, and re-create a new one. Attempts to change it in the annotation ended with DataIntegrityViolationException Commented Mar 31, 2022 at 20:17
  • Thanks for the comment, it seems that's the problem. Commented Apr 1, 2022 at 14:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.