27

To create an index for a collection (as documented here https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/) one can use something like the following:

mongoTemplate.indexOps(Person.class).ensureIndex(new Index().on("name",Order.ASCENDING));

But where in the program should I place this code snippet?

In the relevant repository's constructor? I've did it like that now and it works, but I somehow feel like it is bad design.

Somewhere in Mongo configuration? I haven't found a suitable method to override for that here https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/config/AbstractMongoConfiguration.html

4
  • Have you tried annotating (@Indexed) the indexed field(name) in the person pojo ? Did that not work you or may be that is not what you are looking for ? Commented Nov 1, 2017 at 13:30
  • @Veeram That's not what I am looking for. I have two similar DB model classes, one of which extends the other, and one of these should have a compound unique index over 2 fields, while the other should only have a simple unique index over 1 field. So I've put the @CompoundIndex annotation for the extending class and have to add the index programmatically for the extended class. Commented Nov 2, 2017 at 7:53
  • @Maxim did you found your answer? I have the same doubt. Commented Jan 8, 2018 at 10:09
  • @NishantBhardwaz I've just placed it in the repository's constructor in absence of a better option. Commented Jan 17, 2018 at 8:47

2 Answers 2

44

If you need to do it in programmatic way, you can just create new Spring's @Configuration and perform such initialization:

@Configuration
@DependsOn("mongoTemplate")
public class CollectionsConfig {

    @Autowired
    private MongoTemplate mongoTemplate;

    @PostConstruct
    public void initIndexes() {
        mongoTemplate.indexOps("collectionName") // collection name string or .class
            .ensureIndex(
                new Index().on("name", Sort.Direction.ASC)
        );
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

will this be part of controller, service or repository files ?
This is a separate class and will be called on start.
Thanks Dawid. I found it comfortable to put this as an inner class of the interface of MongoRepository<MyObjectType>
Am I correct in assuming the annotation @DependsOn("mongoTemplate") is not needed in this case because the autowired dependency on MongoTemplate implies the DependsOn relationship with that spring resource?
I recommend following this answer so that @Index annotations can work without needing this. stackoverflow.com/a/62655088/11606132
2

MongoDB supports compound indexes, where a single index structure holds references to multiple fields.

Let's see a quick example using compound indexes:

@QueryEntity
@Document
@CompoundIndexes({
    @CompoundIndex(name = "email_age", def = "{'email.id' : 1, 'age': 1}")
})
public class User {
    //
}

Comments

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.