ujava
ujava

Reputation: 1916

Spring Data MongoDB "_class" bad design pattern

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?

Upvotes: 8

Views: 13444

Answers (1)

Joel Mussman
Joel Mussman

Reputation: 355

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.

Upvotes: 4

Related Questions