Mongo Pojo with Spring boot, how to fetch subset of data in a document

huangapple go评论72阅读模式
英文:

Mongo Pojo with Spring boot, how to fetch subset of data in a document

问题

我有一个Spring Boot应用程序设置,用于与Mongo数据Pojo一起工作。除其他内容外,我有以下依赖关系(我已经排除了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>
    <!-- 排除默认的Jackson依赖 -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
        </exclusion>
    </exclusions>
</dependency>

我有两个类,都引用相同的集合。唯一的区别是ModuleB缺少一些字段-

@Document("Module")
class ModuleA extends MongoModel{
    int X = 0;
    String Y = "something";
}

@Document("Module")
class ModuleB extends MongoModel{
    String Y = "something";
}

我正在使用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);
}

现在,当我使用Spring Repository获取ModuleA或ModuleB的实例/文档时,spring-boot在执行选择操作时是否会有性能差异(或查询差异)?或者在两种情况下,spring-boot是否都会获取所有字段,然后使用所需字段填充对象?

上面的示例可能看起来微不足道,但是如果我有许多字段或Module内部的另一个文档的DBRef,那么与获取整个文档相比,获取子集数据时我可以节省大量的处理。

英文:

I have a spring boot application setup to work with Mongo data Pojo. I have the following dependencies among other things (I have excluded Jackson, and am using Gson instead) -

&lt;dependency&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
	&lt;!-- Exclude the default Jackson dependency --&gt;
	&lt;exclusions&gt;
		&lt;exclusion&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-json&lt;/artifactId&gt;
		&lt;/exclusion&gt;
	&lt;/exclusions&gt;
&lt;/dependency&gt;

I have 2 classes, both referring to the same collection. The only difference is that ModuleB has some fields missing -

@Document(&quot;Module&quot;)
class ModuleA extends MongoModel{
	int X = 0;
	String Y = &quot;something&quot;;
}


@Document(&quot;Module&quot;)
class ModuleB extends MongoModel{
	String Y = &quot;something&quot;;
}

I am using MongoTemplate to get the data -

protected final MongoTemplate template;
@Override
public MongoModel get(Class&lt;? extends MongoModel&gt; cls, Query query) {
	query.addCriteria(Criteria.where(&quot;deleted&quot;).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? Or does spring-boot gets all the fields in both cases anyways, and then populates the object with required fields.

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.

答案1

得分: 1

为了可维护性和一致性,不应该使用多个带有 @Document 注解的模型来引用同一个 MongoDB 集合。

如果一个模型具有不同的 @Index 注解、数据类型等,或者你使用一个模型进行插入,另一个模型进行读取或更新,可能会变得非常混乱。

对于只读操作,这些问题不会出现,但是在这种情况下,你也不需要 @Document 注解,你可以使用投影(projections),就像 @prasad_ 提到的那样。

你应该将投影与核心领域模型放在一起,这样在将来,如果你在核心领域模型中重命名了某个字段,不会忘记在投影中也进行重命名等。

英文:

For maintainability and consistency, you shouldn't use multiple @Document-annotated models referring to the same MongoDB collection.

It can get really messy if one model has e.g. different @Index annotations or datatypes, or you use one model to insert and another model to read or update.

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_.

You should keep the projections together with the core domain model, so in the future you don't forget to e.g. rename a field in the projection after you renamed it in the core domain model etc.

huangapple
  • 本文由 发表于 2020年10月1日 12:03:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64148903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定