Spring-Data mongodb requesting multiple classes stored in the same collection

With Spring-Data, you can use the @Document annotation to indicate which collection to save the object. Say I have two classes: Student and Teacher, both stored in a collection of people. When I execute the following code:

mongo.find(new Query(), Teacher.class);

The result contains both the student and the teacher. In the data created by Spring -Data, each document contains a "_class" field, which indicates which class it is stored from.

Is this a field that is not used in the search as a filter to return only Teachers? How can I request only Teacher, except for this:

mongo.find(new Query().addCriteria(where("_class").is(Teacher.class.getCanonicalName()), Teacher.class);
+5
source share
2 answers

, . Mongo , , . , . , _class DefaultMongoTypeMapper.DEFAULT_TYPE_KEY.

API , , , :

mongo.find(new Query(Teacher.class), Teacher.class);

- , ( , , ). , , , , . Person , , , .

, ( , Object, ), , . @Document.

: , JIRA, , .

+1

:

query.restrict(Teacher.class,Teacher.class);
+1

All Articles