简体   繁体   English

在 Spring.Mongodb 中按日期间隔聚合 Mongo 模板

[英]Mongo Template Aggregate by a date interval in Spring.Mongodb

I need to aggregate some data using java and Mongodb.我需要使用 java 和 Mongodb 聚合一些数据。

So my JS script it's that:所以我的 JS 脚本是这样的:

db.post.aggregate([
    {$match: {date: {$gte: ISODate("2019-08-28T17:50:09.803Z"), $lte: ISODate("2019-12-03T21:45:51.412+00:00")}}},
    {$project: {author: 1, _id: 0}},
    {$group: {_id: "$author", count: {$sum:1}}}])

And my Java Code using spring+java is:我使用 spring+java 的 Java 代码是:

    Aggregation aggregation =
            newAggregation(
                    match(Criteria.where("date")
                            .lte(dynamicQuery.getEndDate())
                            .gte(dynamicQuery.getInitDate())),
                    project("author").andExclude("_id"),
                    group("author")
                            .count()
                            .as("total"));

    AggregationResults<Post> result = mongoTemplate.aggregate(aggregation,Post.class, PostSummary.class);
    List<Post> map = result.getMappedResults();

And I need to aggregate the sum of documents by authorId.我需要按authorId聚合文档的总和。 My code returns the user code and no sum of documents.我的代码返回用户代码,没有文档总和。

And we have 2 Collections:我们有 2 个集合:

@EqualsAndHashCode(of = "id")
//TODO: Change the @Data to the properly scenario, we don't need 
setters in this class anymore.
@Data
@Document (collection = "user")
//TODO: Change collection name to post, because collection are a group 
of elements
public class User {

  @Id
  private String id;

  private String name;

  private String username;

  @Email
  private String email;

  private String imageUrl;

  private String providerId;

  private LocalDateTime firstAccess;

} }

Post Document:邮寄文件:

@Data
@Document(collection = "post")
//TODO: Change collection name to post, because collection are a group of elements
public class Post {
   public static final String AUTHOR_FIELD_NAME = "author";
   public static final String DATE_FIELD_NAME = "date";

   @Id
   private String id;
   private String content;

   @DBRef(lazy = true)
   @Field(AUTHOR_FIELD_NAME)
   private User author;

   @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
   @Field(DATE_FIELD_NAME)
   private LocalDateTime date;

   public Post(String content, User author, LocalDateTime date) {
       this.content = content;
       this.author = author;
       this.date = date;
    }
  }

I have one solution, but I do not know if it is a better solution, so, let's go.我有一个解决方案,但我不知道它是否是更好的解决方案,所以,让我们开始吧。

Because the Entity represented inside the Post Domain is one DBRef, the MongoDB does not parse the entire Stringo to the PostSummary.Class因为 Post 域内表示的 Entity 是一个 DBRef,所以 MongoDB 不会将整个 Stringo 解析为 PostSummary.Class

    @Getter
    @Setter
    public class PostSummary {
        private User author;
        private int count;
    }

So, for the solution, I only parse the @Id in the author, and it's working.所以,对于解决方案,我只解析了作者中的@Id,它正在工作。 So If anyone has a better solution we have one solution now.所以如果有人有更好的解决方案,我们现在就有一个解决方案。

And should be like this:应该是这样的:

  @Getter
  @Setter
  public class PostSummary {
    @Id
    private User author;
    private int count;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM