[英]Mongo Pojo with Spring boot, how to fetch subset of data in a document
I have a spring boot application setup to work with Mongo data Pojo.我有一个 Spring Boot 应用程序设置来处理 Mongo 数据 Pojo。 I have the following dependencies among other things (I have excluded Jackson, and am using Gson instead) -
除其他外,我还有以下依赖项(我排除了 Jackson,而是使用 Gson)-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Exclude the default Jackson dependency -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
I have 2 classes, both referring to the same collection.我有 2 个类,都指向同一个集合。 The only difference is that ModuleB has some fields missing -
唯一的区别是 ModuleB 缺少一些字段 -
@Document("Module")
class ModuleA extends MongoModel{
int X = 0;
String Y = "something";
}
@Document("Module")
class ModuleB extends MongoModel{
String Y = "something";
}
I am using MongoTemplate to get the data -我正在使用 MongoTemplate 来获取数据 -
protected final MongoTemplate template;
@Override
public MongoModel get(Class<? extends MongoModel> cls, Query query) {
query.addCriteria(Criteria.where("deleted").is(0));
return template.findOne(query, cls);
}
Now, when I use spring repository to get an instance/document of ModuleA or ModuleB, is there going to be a performance difference (or query difference) in the select operation that is performed by spring-boot?现在,当我使用 spring 存储库获取 ModuleA 或 ModuleB 的实例/文档时,spring-boot 执行的选择操作是否会有性能差异(或查询差异)? Or does spring-boot gets all the fields in both cases anyways, and then populates the object with required fields.
或者 spring-boot 是否在这两种情况下都获取所有字段,然后使用所需字段填充对象。
The example above may seem trivial, but if I have many fields or DBRef to another document inside the Module, I can save significant processing when fetching a subset of data instead of fetching the entire document.上面的例子可能看起来微不足道,但如果我有很多字段或 DBRef 到模块内的另一个文档,我可以在获取数据子集而不是获取整个文档时节省大量处理。
For maintainability and consistency, you shouldn't use multiple @Document-annotated models referring to the same MongoDB collection.为了可维护性和一致性,您不应使用多个 @Document 注释模型来引用同一个 MongoDB 集合。
It can get really messy if one model has eg different @Index annotations or datatypes, or you use one model to insert and another model to read or update.如果一个模型有不同的@Index 注释或数据类型,或者您使用一个模型插入而另一个模型读取或更新,它会变得非常混乱。
For read-only operations those problem do not occur, but then you don't need a @Document annotation anyway, you can use projections , as mentioned by @prasad_.对于只读操作,这些问题不会发生,但是无论如何您都不需要@Document 注释,您可以使用投影,如@prasad_ 所述。
You should keep the projections together with the core domain model, so in the future you don't forget to eg rename a field in the projection after you renamed it in the core domain model etc.您应该将投影与核心域模型保持在一起,因此将来您不要忘记在核心域模型中重命名投影中的字段等后重命名投影中的字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.