Uploaded image for project: 'Spring Data MongoDB'
  1. Spring Data MongoDB
  2. DATAMONGO-2190

Support For Creating Index in MongoTemplate based On Repository

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Resolved
    • Priority: Minor
    • Resolution: Duplicate
    • Affects Version/s: 2.1.4 (Lovelace SR4)
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:
      Spring Boot 2.1 on Ubuntu

      Description

      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.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              mp911de Mark Paluch
              Reporter:
              ankur pathak Ankur Pathak
              Last updater:
              Mark Paluch
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: