Spring Data MongoDB "_class" bad design pattern

10,821

Solution 1

I don't know what leads you to the assumption to improve the chances of getting advice if you're bashing the project your seeking advice for. Beyond that, when using a software library you might wanna assume that implementation details have been thought through and are the way they are for a reason.

If you had taken the time to consider read up the reference documentation you would have discovered the section explaining why the _class attribute is there in general as well as how to tweak the values written, how to customize the key or even turn off the type information written at all.

After reading up that stuff you can now go back to write perfectly fine software again in turn. Thanks!

PS: I've taken a bit of time to polish up your badly designed way of asking a question. ;)

Solution 2

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.

Share:
10,821
ujava
Author by

ujava

Updated on June 18, 2022

Comments

  • ujava
    ujava about 2 years

    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?