简体   繁体   English

使用 Spring 启动的 Mongo Pojo,如何获取文档中的数据子集

[英]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.

相关问题 Spring Data Mongo-如何映射继承的POJO实体? - Spring Data Mongo - How to map inherited POJO entities? 如何使用spring mongo data api更新嵌入式mongo文档 - How to update embedded mongo document using spring mongo data api 在没有实体或 POJO 类的情况下,使用 JPA 和 Spring Boot 从数据库中获取数据? - Fetch Data from Database using JPA and Spring Boot without entity or POJO class? 从 spring 启动应用程序中的 mongo db 获取数据,其中要获取的集合名称和字段在运行时是已知的 - Fetch data from mongo db in spring boot application where collection name & fields to fetch are known at runtime Spring 启动Mongo数据-如何设置命名策略 - Spring Boot Mongo Data - How to Set Naming Strategy 如何使用Spring Boot和Mongo更新Mongo中的数据并做出反应 - How to update data in mongowith spring boot and mongo and react 如何使用 Spring Data Mongo DB 仅检索文档的特定字段? - How to retrieve only specific field of a document with Spring Data Mongo DB? 如何在不丢失 Spring Mongotemplate 项目中的数据的情况下更新 Mongo 中的文档 - How to update document in Mongo without loosing data in a Spring Mongotemplate project 如何使用Spring数据计算Mongo文档嵌套数组中的项目? - How to count items in nested array of mongo document using spring data? 如何根据 spring 引导中的两个日期从 mongoDB 获取数据? - How to fetch data from mongoDB based on two dates in spring boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM