0

I'm using Kotlin with Spring to manage a MongoDB collection. I've noticed that every new document comes with _class field built in.

Is there an elegant way to get rid of it? I don't wanna store any bit of data that's not completely necessary.

I saw some Java solutions for that but wanted to ask if you guys know about any elegant solution in Kotlin.

Thanks

user2101699
  • 207
  • 3
  • 16
  • Duplicate from this? https://stackoverflow.com/questions/23517977/spring-boot-mongodb-how-to-remove-the-class-column/48546964 – Dimitris Chasanidis Oct 08 '20 at 10:37
  • @DimitrisChasanidis although it is the same question, most people won't consider that answer as acceptable, since that will create quite a few issues when reading back the data. The question for OP is, are you planning on reading back the data with the same model you wrote it? If yes, then you are better of by leaving that `_class` field alone. If you delete the "magic" you won't be able to use the "magic". – AlexT Oct 08 '20 at 10:41
  • @Alex thanks for the answer! What i'm afraid of is a future refactor that will require me to go over all of the documents and change them or make the current class redundant – user2101699 Oct 08 '20 at 10:47
  • 1
    if the future reading of the data will be done with spring as well, you can refactor at your hearts will, it won't affect it. What will affect it is removing the `_class`, which will override the `MappingMongoConverter`. You can to a small test by putting mock data on the db, adding a field (with a default value of course), or removing a field (and set jackson to ignore unknown values) and then reading it back. You'll see that it will work and give you the peace of mind you need to go forwards. (btw, I'm not the biggest fan of spring, but it does what it does ok). – AlexT Oct 08 '20 at 10:57

0 Answers0