Search code examples
mongodbspring-data-mongodbmongo-java

How to update existing documents with Spring Data MongoDB type information after type renames?


I am using mongodb with spring and I have some subdocuments that are derived from one base class, mongo uses the '_class' property to find out what is the correct type to convert to, but now I have changed the package name and the collection still has documents with old package name, how can I tell mongo what custom conversions to do ?


Solution

  • first of all for next time to decouple your package/class name from _class variable, use: @TypeAlias("name") annotation on your pojo. then _class will have the value "name"

    The easiest solution in my opinion is to update your pojo with TypeAlias and update your collection's _class field with it's new value

    Read about Type mapping over here: http://docs.spring.io/spring-data/data-mongodb/docs/current/reference/html/#mongo-template.type-mapping