Spring-Data mongodb查询存储在同一集合中的多个类 [英] Spring-Data mongodb querying multiple classes stored in the same collection

查看:140
本文介绍了Spring-Data mongodb查询存储在同一集合中的多个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Spring-Data,您可以使用@Document批注指定将对象保存到的集合.假设我有两个班级,学生"和老师",两个班级都坚持加入了人员集合.当我执行以下代码时:

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

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

结果同时包含学生"和老师".查看由Spring-Data创建的数据,每个文档都包含一个"_class"字段,该字段指示该持久化自哪个类.

the result contains both Student and Teacher. Looking in the data created by Spring-Data, each document contains a "_class" field which indicate which class it is persisted from.

在查找中不使用此字段作为仅返回教师的过滤器吗?除了执行此操作外,我如何仅查询教师:

This is field not used in find as an filter to return only Teacher? How do I query for only Teacher other than doing this:

mongo.find(new Query().addCriteria(where("_class").is(Teacher.class.getCanonicalName()), Teacher.class);

推荐答案

这或多或少是一个关于如何设计收藏集的问题.由于Mongo对类型一无所知,因此我们必须添加其他元数据以能够将文档彼此区分开.因此,在查询时,您还需要将这些约束添加到查询中.请注意,您可以通过DefaultMongoTypeMapper.DEFAULT_TYPE_KEY引用_class键.

This is more or less a question of how to design your collections. As Mongo does not know anything about types we have to add the additional metadata to be able to distinguish documents from each other. Thus when querying you'll need to add these constraints to the query as well. Note that you can refer to the _class key through DefaultMongoTypeMapper.DEFAULT_TYPE_KEY.

我们曾考虑向查询中添加一些API,以表示只希望获取给定类型的文档,如下所示:

We thought about adding some API to the query to express one only wants to get documents of a given type like this:

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

仍然感到有些奇怪,您必须两次声明域类(如果您想将结果限制为给定类型的文档,但将它们映射到另一个类,这是有道理的).除此之外,我们只能将结果限制为带有正是类型的文档,因为类型存储为字符串,并且继承只能在已加载该类的情况下应用.因此,当查询类型为Person的文档时,我们必须首先加载 all 个文档,尝试查找类型,进行类型检查,并可能丢弃性能欠佳的结果原因.

It still feels a bit weird that you have to state domain class twice (which makes sense if you want to restrict the results to documents of a given type but map them onto an different class). Beyond that we could only restrict the results to documents that carry exactly that type as the types are stored as Strings and inheritance can only be applied if the class has already been loaded. So when querying for documents with type Person we'd have to load all documents first, try to lookup the type, do the type check and potentially throw away the result which is sub-optimal for performance reasons.

另一种选择是存储可分配给一个类的所有类型(可能是除Object以外的几乎所有接口和超类),但是这会导致存储大量数据.可以通过在@Document批注中添加一个标志来解决这个问题.

Another option would be to store all types a class is assignable to (pretty much all interfaces and superclasses except Object maybe) but that would cause quite an amount of data being stored. This could be in turn approached by adding a flag to the @Document annotation.

最重要的是:目前,如果您有改善建议的建议,别无选择,但可以随时提高JIRA门票的价格.

Bottom line: currently, there's no other way but feel free to raise JIRA tickets if you have suggestions how this can be improved.

这篇关于Spring-Data mongodb查询存储在同一集合中的多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆