4

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 ?

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
jacob
  • 1,397
  • 1
  • 26
  • 53

1 Answers1

8

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

royB
  • 12,779
  • 15
  • 58
  • 80
  • Sometime you need with the `@TypeAlias` to add `@Document(collection="collectionName")` for it to work. I encountered the same problem [here](https://stackoverflow.com/q/57910383/7747942) – Sylhare Sep 12 '19 at 20:23