【问题标题】:Mongo Template Aggregate by a date interval in Spring.MongodbMongo模板在Spring.Mongodb中按日期间隔聚合
【发布时间】:2019-12-03 18:50:40
【问题描述】:

我需要使用 java 和 Mongodb 聚合一些数据。

所以我的 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}}}])

而我使用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();

我需要按 authorId 汇总文档的总和。我的代码返回用户代码并且没有文档总和。

我们有 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;

}

发布文件:

@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;
    }
  }

【问题讨论】:

  • 可能是你的组和项目在newAggregation函数中的顺序,重新排序项目和组
  • 对不起,缺少投影代码,我会在代码中添加。
  • 可以贴出你的样本采集记录,方便我本地执行查看。
  • 是的,我在这里发给你
  • { "_id" : ObjectId("5da9e69ee8222321bc76ac57"), "content" : "Hello Friday", "author" : DBRef("user", ObjectId("5da9e66ce8222321bc76ac56")), "date" : ISODate("2019-10-18T16:21:50.631Z"), "_class" : "com.avenuecode.magpie.domain.Post" }

标签: java spring mongodb aggregation-framework


【解决方案1】:

我有一个解决方案,但不知道是不是更好的解决方案,所以,我们走吧。

因为 Post Domain 内部表示的 Entity 是一个 DBRef,所以 MongoDB 不会将整个 Stringo 解析到 PostSummary.Class

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

所以,对于解决方案,我只解析作者中的@Id,它正在工作。因此,如果有人有更好的解决方案,我们现在就有一个解决方案。

应该是这样的:

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

【讨论】:

    猜你喜欢
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 2014-08-23
    • 2021-09-03
    • 2020-05-09
    • 1970-01-01
    相关资源
    最近更新 更多