【问题标题】:Pagination with Aggregations - Get total count聚合分页 - 获取总数
【发布时间】:2019-08-29 03:20:14
【问题描述】:

我有员工数据的 mongo 集合 - 需要在应用过滤器后将数据带入分页,这适用于聚合 - 但我缺少现有的员工总数。

我尝试了 FacetOperation - 它不允许分组操作或计数操作。我有工作的 mongo 查询,它可以正确地为我提供数据 - 我需要将其转换为 spring 数据

db.data.aggregate([
{
  "$facet": {
    "totalData": [
      {
        "$match": {
          "DatabaseId": "Abcdefg"
        }
      },
      {
        "$skip": 0
      },
      {
        "$limit": 15
      },
      {
        "$sort": {
          "typeCount.error": 1
        }
      },
      {
        "$project": {
          "id": 1,
          "personalData": 1,
          "typeCount": 1,
          "messages": 1,
          "updatedDate": 1,
          "updatedBy": 1
        }
      }
    ],
    "totalCount": [
      {
        "$count": "count"
      }
    ]
  }
}
])

我有这样的 Spring 数据

Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(
                        Criteria.where("DatabaseId").is(Abcdefg)),
                Aggregation.skip(filter.page * filter.pageSize as long),
                Aggregation.limit(filter.pageSize),
                Aggregation.project("id",
                        "personalData",
                        "typeCount",
                        "messages",
                        "updatedDate",
                        "updatedBy",
                        ))

现在我需要将最后一部分添加到此代码中:获取总数

【问题讨论】:

    标签: mongodb spring-data


    【解决方案1】:

    这是我在代码中使用的内容, 先做一个转换类

    public class CustomOperation implements AggregationOperation {
    
        private Document document;
    
        public CustomOperation(Document document) {
            this.document = document;
        }
    
        @Override
        public Document toDocument(AggregationOperationContext aggregationOperationContext) {
            return aggregationOperationContext.getMappedObject(document);
        }
    }
    
    

    现在你可以这样写你的操作符了

    Document facet = new Document().append("$facet",new Document(
                    "totalDate",Arrays.asList(
                            new Document("$match", new Document("DatabaseId","Abcdefg")),
                    new Document("$skip",0),
                    new Document("$limit",15),
                    new Document("$sort",new Document("typeCount.error",1)),
                    new Document("$project",
                            new Document("id",1)
                                    .append("personalData",1)
                            .append("typeCount",1)
                            .append("messages",1)
                            .append("updatedDate",1)
                            .append("updatedBy",1))
            )).append("totalCount",Arrays.asList(new Document("$count","count")))
            );
            CustomOperation facetOp = new CustomOperation(facet);
            Aggregation aggregation = Aggregation.newAggregation(facetOp);
    
    
    

    希望对你有所帮助

    【讨论】:

    • 谢谢你的帮助,我们如何在这个文档中添加查找操作?
    • @ReddyG 您要在其中进行查找操作,如果它是聚合的另一个操作,则 new Document() 并将其添加到 Aggregation.newAggregation() 中,或者如果它是 Facet 操作的运算符,只需执行同样的东西,并在Arrays.asList() 中使用逗号分隔运算符
    猜你喜欢
    • 2017-04-04
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    相关资源
    最近更新 更多