8

I have problems with Spring Data MongoDB project. My problem is _class property in all collection records. My user collection size is nearly 1.3 million records. All records have a _class property. This is problem and bad design for project. Because MongoDB is a document-based system. Size is a problem each record in the collections. User collection:

{ "_class" : "com.myproject.xxx.yy.aaa.bb.User", … }

What if I want to move the User class to another package? Why does Spring Data add a _class property to all records?

Charles
  • 50,943
  • 13
  • 104
  • 142
ujava
  • 1,846
  • 5
  • 20
  • 24
  • http://forum.springsource.org/showthread.php?112505-Spring-data-MongoDb-MappingMongoConverter-remove-_class – practical programmer Aug 18 '12 at 14:06
  • 2
    To avoid inconsistency while moving the class to another package you can use [@TypeAlias](https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo-template.type-mapping) – O.Badr Oct 17 '20 at 20:41
  • Does this answer your question? [Spring data MongoDb: MappingMongoConverter remove \_class](https://stackoverflow.com/questions/6810488/spring-data-mongodb-mappingmongoconverter-remove-class) – Étienne Miret Feb 03 '23 at 03:55

1 Answers1

3

For people visiting this more recently, look at the @TypeAlias annotation that allows you to declare the value that will be put in _class, and therefore a smaller value can be used.

This should partially answer @Eric2201's question above too, where he asks about a different type attribute. I'm not sure that having a different and therefore inconsistent attribute would be a good idea, I am happy with _class. You can of course add any field that you want to the document though.

Nobody asked about it here, but just FYI to complete this you can mix types in the same collection. If you want to restrict a document search based on type it doesn't happen automatically in Spring Mongo even though MongoTemplate asks for the type in the "find" methods; you have add _class to the query yourself.

Joel Mussman
  • 196
  • 1
  • 5