【发布时间】:2018-06-16 19:31:04
【问题描述】:
我有两个非常简单的实体,带有 1 -> * 'relation' 的 Post 和 Comments。
这是我的实体:
@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Comment {
@Id
private String id;
@JsonProperty(access = READ_ONLY)
@Indexed
private String postId;
@NotEmpty
@Length(max = 300)
private String description;
@JsonProperty(access = READ_ONLY)
private Instant createdDate;
}
@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Post {
@Id
@JsonProperty(access = READ_ONLY)
private String id;
@NotEmpty
@Length(max = 300)
private String title;
@NotEmpty
private String description;
@JsonProperty(access = READ_ONLY)
private Instant createdDate;
@JsonProperty(access = READ_ONLY)
private Instant updatedDate;
@JsonProperty(access = READ_ONLY)
private Long commentsCount = 0L;
}
如您所见,我的 Post 实体具有您认为的“cmetsCount”字段。在通过帖子存储库获取单个帖子实例/帖子列表时是否可以以某种方式填充此字段?没错,我正在通过评论的存储库对每个 postId 的计数进行额外的查询,而对于 20 个帖子来说,这并不好,我的响应时间从 10 毫秒增加到 30-40 毫秒。
【问题讨论】:
标签: spring mongodb spring-data-mongodb