首页 > 解决方案 > 带分页的 MongoDB 聚合查询

问题描述

我有一个聚合查询

db.getCollection('sell_ProductionRecords').aggregate([
   
    { $lookup : {
       'from': "sell_ProduceRecords",
       'localField': "produceID",
       'foreignField': "_id",
       'as': "produce"
        }},
     { $lookup : {
       'from': "sell_PackageType",
       'localField': "packagingTypeID",
       'foreignField': "_id",
       'as': "packageType"
        }}   ,
         { $lookup : {
       'from': "sell_Plots",
       'localField': "plotID",
       'foreignField': "_id",
       'as': "plots"
        }},
     { $match: { 'plots.plotNumber': "123457" } } ,
       {$facet: {
        result: [
           { $skip: 0 },
           { $limit: 1 }
           ],
        totalCount: [
      {
        $count: 'count'
      }
    ]
  }}
])

我正在尝试为此创建一个聚合查询

Aggregation  aggregation = Aggregation.newAggregation(
                    Aggregation.lookup("sell_ProduceRecords", "produceID", "_id", "produce"),
                    Aggregation.lookup("sell_Plots", "plotID", "_id", "plots"),
                    Aggregation.lookup("sell_PackageType", "packagingTypeID", "_id", "packageType"),
                    Aggregation.match(new Criteria().andOperator(criteria.toArray(new Criteria[criteria.size()]))),
                    sort,
                 Aggregation.facet(
                        Aggregation.skip(paging.getPageNumber() * paging.getPageSize()),
                        Aggregation.limit(paging.getPageSize()
                        )).as("result")


        );

但我不知道如何在 facet 中创建总计数。上面的查询对我来说很好,但我不知道如何在 java 中为此创建聚合

标签: mongodbaggregation-framework

解决方案


推荐阅读