Skip to content

Lefeverw/stackoverflow-spring-mongodb-update-index

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

stackoverflow-spring-mongodb-update-index

Example for question: https://stackoverflow.com/questions/76878180/how-to-update-index-in-mongodb-when-using-spring

Steps for migration

  1. Insert data with the original index

In application.yml point mongock.migration-scan-package to a non-existing package (e.g. empty).

mongock:
  migration-scan-package:
    - empty
#    - com.wouterlefever.stackoverflow.changeset

In the entity Person use the initial index:

@CompoundIndex(
        name = "unique_name_with_age",
        def = "{ 'firstName':1 , 'lastName':1 }",
        unique = true)
//@CompoundIndex(
//        name = "unique_name_with_age",
//        def = "{ 'firstName':1 , 'lastName':1, 'ssn':1 }",
//        unique = true)
@Document
public class Person {

And run the application.

  1. Delete the existing index during migration.

Comment out the insert of data in Application

    @Override
    public void run(String... args) {
//        Person person = new Person("John", "Doe", "1");
//        personRepository.save(person);
    }

Comment out the index definitions in Person.

//@CompoundIndex(
//        name = "unique_name_with_age",
//        def = "{ 'firstName':1 , 'lastName':1 }",
//        unique = true)
//@CompoundIndex(
//        name = "unique_name_with_age",
//        def = "{ 'firstName':1 , 'lastName':1, 'ssn':1 }",
//        unique = true)
@Document
public class Person {

In application.yml point mongock.migration-scan-package to the changeset package.

mongock:
  migration-scan-package:
#    - empty
    - com.wouterlefever.stackoverflow.changeset

And run the application.

  1. Add the new index

Add the new index to the entity Person

@CompoundIndex(
        name = "unique_name_with_age",
        def = "{ 'firstName':1 , 'lastName':1, 'ssn':1 }",
        unique = true)
@Document
public class Person {

And run the application.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages