Skip to content

Support For Creating Index in MongoTemplate based On Repository [DATAMONGO-2190] #3051

Closed
@spring-projects-issues

Description

@spring-projects-issues

Ankur Pathak opened DATAMONGO-2190 and commented

I have my mongo repository like this:
|@QueryEntity
@Document
public class User {
    ``@Indexed
    ``private String name;
     
     {{... }
}
}|

I create Index programatically using MongoTemplate like this:

 
mongoOps.indexOps(User.``class``).
  ``ensureIndex(``new Index().on(``"name"``, Direction.ASC));

Why I have to give Index infromation while creating 
Index programtically using Mongo Template?
The below information in above method called is redundant:

new Index().on(``"name"``, Direction.ASC)

as same information is already on my Repositry by:

@Indexed
    ``private String name;

So actual call should be like:

mongoOps.indexOps(User.``class``).``ensureIndex()

We can have above version of ensureIndex to add more Indexes
programatically not specified on Repository.

But version of ensureIndex which takes Index information
from repository and create Indexes is a must.

This way of creating Indexes is also very helpful during tests else ours
test keep on failing with DuplicateKeyExcepton. Please check this
article for reference:
https://dzone.com/articles/spring-integration-tests
I tried this way of this article but if also failing because of API Changes?
Can you provide quick fix or some solution for this use case


Affects: 2.1.4 (Lovelace SR4)

Reference URL: https://dzone.com/articles/spring-integration-tests

Issue Links:

  • DATAMONGO-2188 Add configuration option to enable/disable automatic index creation

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions