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?

3

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.